On 21/09/17 16:12, SG wrote:
> On Thursday, September 21, 2017 at 3:05:00 PM UTC+2, David Brown wrote:
>>
>> void f3(void) {
>> auto p = std::tie(x, y);
>> std::tie(y, x) = p;
>> }
>> // Same code as just "x = y";
>>
>> void f4(void) {
>> std::tie(x, y) = std::tie(y, x);
>> }
>> // Same code as f3, i.e., just "x = y";
>
> As Chris Vine already said, std::tie(x,y) gives you a tuple<int&,int&>
> instead of a tuple<int,int>. This is necessary for the pattern with
> tie(...) as the target of an assignment to work as intended.
>
> What you could write instead is
>
> tie(x,y) = make_tuple(y,x);
Yes, I realised that after reading Chris's post, and shortly before you
made your reply.
I had just been playing around with tuples and ties, and got myself
confused.
>
> Here, make_tuple creates a tuple<int,int> with copied values. In C++17
> you can also use tuple's constructor like this
>
> tie(x,y) = tuple(y,x);
>
> because this class template argument deduction gives you a tuple of
> values, too.
>
C++17 also allows:
auto [a, b] = std::tuple(y, x);
std::tie(x, y) = std::tuple(a, b);
(And one of these "std::tuple" can be "std::tie", but not both. It all
makes sense now.)
> Cheers!
> SG
>