can we do deep tie with a c++1y std::tie() -like function?

三世轮回 提交于 2019-12-03 17:19:37

Maybe you're looking for std::tuple_cat:

std::tie(x,y,z) = std::tuple_cat(t, make_tuple(3)); 

You can string together tuples as 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::tie works (I think). std::tie constructs 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 to std::tie is 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.

You can go:

std::forward_as_tuple(std::tie(x, y), z) = std::make_tuple(t, 3);

std::forward_as_tuple works very similarly to std::tie, except that unlike it it will not reject std::tie(x, y) as an argument.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!