I frequently write snippets like
int x,y,z; tie(x,y,z) = g[19];
where, for instance, g
was declared earlier
vector<tuple<int,int,int>> g(100);
Problem is, maybe later I actually want x
and y
to point to the internals of g
by reference, and the refactoring is ugly, e.g.
int &x = get<0>(g[19]);
int &y = get<1>(g[19]);
int &z = get<2>(g[19]);
or sometimes even worse, for instance if the access is a more complex expression
tuple<int,int,int> &p = g[19]; // if the rhs was actually more complicated
int &x = get<0>(p);
int &y = get<1>(p);
int &z = get<2>(p);
Is there a better refactoring, more in the style of the assignment to tie(..)?
The difficulty as I understand it is that references insist on being initialized exactly at their declaration. So, in possibly other words, is there a way to use tie
-like syntax for multiple variable initialization in c++ (this would also make the earlier non reference usage cleaner)?