Is there a way to write a variant of std::tie in c++11/1y that ties deeply into a tuple. That is, one in which tie((x,y),z) = make_tuple(make_tuple(1,2),3) binds x, y, z to 1, 2 and 3, respectively as in the following example. It would be nice. Thanks.
#include <tuple>
#include <iostream>
using namespace std;
int main() {
int x, y ,z;
auto t = make_tuple(1,2);
std::tie(y,x)= t;
//std::tie((x,y),z) = make_tuple(t,3); //not working
cout << x << y << z << endl;
return 0;
}
Maybe you're looking for
std::tuple_cat:You can string together
tuplesas one long tuple, to avoid dealing with nested tuples. I think the solution to flattening nested tuples would be more complex.Just to make a clarification on how
std::tieworks (I think).std::tieconstructs a tuple of lvalue references from its arguments. When you use the assignment operator, copy assignments are performed.std::tie((x,y),z)doesn't do what you think. You're subjecting(x,y)to the comma operator, where x is discarded. There is no magic going on, where nesting is determined by parentheses. If one of the arguments tostd::tieis a tuple, then the corresponding argument should be a tuple as well. i.e.:std::tie(tuple, 3) = std::make_tuple(std::make_tuple(1, 2), 3). However this is not what you want, which is where my suggestion comes from, because it doesn't seem like your intention is to flatten a nested tuple.