c++ - How to make a tuple of lvalue references from a tuple of values -


there questions relate topic:

how make tuple of const references?

std::make_tuple doesn't make references

but neither discusses how make tuple of lvalue references from tuple of values.

here i've got:

template <typename... args> std::tuple<args&...> maketupleref(const std::tuple<args...>& tuple) {     return std::tie(tuple); // fails because std::tie expects list of arguments, not tuple. }  int main() {     std::tuple<int, int> tup;     std::tuple<int&, int&> tup2 = maketupleref(tup); // values of tup2 should refer in tup     return 0; } 

as far can tell std::tie ideal here because produces lvalue references, doesn't accept tuple input. how can around problem?

the usual integer_sequence trick:

template <typename... args, std::size_t... is> std::tuple<args&...> maketupleref(std::tuple<args...>& tuple, std::index_sequence<is...>) {     return std::tie(std::get<is>(tuple)...); }   template <typename... args> std::tuple<args&...> maketupleref(std::tuple<args...>& tuple) {     return maketupleref(tuple, std::make_index_sequence<sizeof...(args)>()); } 

there's simpler alternative if types in tuple known unique:

template <typename... args> std::tuple<args&...> maketupleref(std::tuple<args...>& tuple) {     return std::tie(std::get<args>(tuple)...); } 

Comments

Popular posts from this blog

rdbms - what exactly the undo information lives in oracle? -

bash - How do you programmatically add a bats test? -

clojure - 'get' replacement that throws exception on not found? -