Michael Michael - 14 days ago 5
C++ Question

Unclear syntax with std::tie

I'm just reading a bit about tuples.

Now the following syntax is not clear to me:

std::tie (myint, std::ignore, mychar) = mytuple;


It's not difficult to grasp what it does, BUT what happens from a language point of view? We are somehow assigning to the return value of a function?

hvd hvd
Answer

BUT what happens from a language point of view? We are somehow assigning to the return value of a function?

Yes, that can be valid, depending on the function's return type. There are mainly two ways in which it can be valid: firstly, the function may return an lvalue reference to an object.

int i;
int &f() { return i; }
int main() { f() = 1; } // okay, assigns to i

Secondly, functions may return user-defined types with an = operator implementation that can be called on rvalues:

struct S { void operator=(int) { } };
S f() { return {}; }
int main() { f() = 1; } // okay, calls S::operator=

The latter is what's happening with std::tie.

Comments