Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #83012
| From | Manfred <noname@add.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Structured binding: referencing an existing variable |
| Date | 2022-02-16 21:43 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <sujnm3$1eda$1@gioia.aioe.org> (permalink) |
| References | <sui6ki$p37$1@dont-email.me> |
On 2/16/2022 7:46 AM, Paavo Helde wrote:
>
> I was cleaning up some code and getting rid of an obscure in-out
> reference parameter by replacing it by a tuple return. E.g. with a
> simplified example, instead of former
>
> A foo(int& pos_in_out);
I'm not sure I get how a byref parameter is obscure, but anyway...
>
> we now have
>
> std::pair<A, int> foo(int pos_in);
>
> where the updated pos is returned explicitly.
>
> Now how do I capture these results when calling it? In the old call it was:
>
> int pos = 0;
> while(pos < N) {
> A a = foo(pos);
> // ...
> }
>
> How should I replace it? The thing is A does not have a default ctor and
> should be created afresh each time when foo is called, while pos should
> be updated instead.
>
> std::tie(a, pos) = foo(pos); // Does not work for A as it cannot be
> pre-constructed.
>
> An alternative is to use C++17 structured binding:
>
> auto [a, pos] = foo(pos); // Does not work for pos as it creates a new
> variable, instead of updating the old one.
>
> In short, I would want to use structured binding for A and effectively
> std::tie() for the other member. Is that somehow possible?
>
> For now I resorted to:
>
> auto [a, newPos] = foo(pos);
> pos = newPos;
>
> but this introduces an unneeded extra variable and does not look like so
> great improvement over the old code.
Hmm.
So far it looks like the original "problem"
> A foo(int& pos_in_out);
is not worse than the solution ;)
More to the point, if you really don't want the reference argument in
foo(), I'd probably look closer at two options:
1) See if std::pair<A, int> is a type that may actually belong in the
calling context, and use that directly [*], not as a temp to be split
after the call.
2) Look closer at why an A() ctor is not acceptable, which is what stops
std::tie from working . I'm not sure that std::tie makes for clearer
code than the reference argument, though.
[*] it might be argued that the number and type of the variables being
used are what constitutes the state of a system, so changing two
separate int and A variables into a combined tuple does have some impact
on the model of the system. Then the question becomes if this tuple
yields a better model of the system at hand than two separate variables.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Structured binding: referencing an existing variable Paavo Helde <eesnimi@osa.pri.ee> - 2022-02-16 08:46 +0200
Re: Structured binding: referencing an existing variable Manfred <noname@add.invalid> - 2022-02-16 21:43 +0100
Re: Structured binding: referencing an existing variable Vir Campestris <vir.campestris@invalid.invalid> - 2022-02-16 22:00 +0000
Re: Structured binding: referencing an existing variable Paavo Helde <eesnimi@osa.pri.ee> - 2022-02-17 16:00 +0200
csiph-web