Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::make_tuple doesn't make references

I've been experimenting with std::tuple in combination with references:

#include <iostream>
#include <tuple>

int main() {
  int a,b;
  std::tuple<int&,int&> test(a,b);
  std::get<0>(test) = 1;
  std::get<1>(test) = 2;
  std::cout << a << ":" << b << std::endl;

  // doesn't make ref, not expected
  auto test2 = std::make_tuple(a,b);
  std::get<0>(test2) = -1;
  std::get<1>(test2) = -2;
  std::cout << a << ":" << b << std::endl;

  int &ar=a;
  int &br=b;
  // why does this not make a tuple of int& references? can we force it to notice?
  auto test3 = std::make_tuple(ar,br);
  std::get<0>(test3) = -1;
  std::get<1>(test3) = -2;
  std::cout << a << ":" << b << std::endl;
}

Of the three examples here the first two work as expected. The third one however does not. I was expecting the auto type (test3) to be the same as the type of test (i.e. std::tuple<int&,int&>).

It seems that std::make_tuple can't automatically make tuples of references. Why not? What can I do to make this the case, other than explicitly constructing something of that type myself?

(Compiler was g++ 4.4.5, using 4.5 doesn't change it)

like image 566
Flexo Avatar asked Oct 23 '11 15:10

Flexo


3 Answers

std::tie makes non-const references.

auto ref_tuple = std::tie(a,b); // decltype(ref_tuple) == std::tuple<int&, int&>

For const references, you'll either want the std::cref wrapper function:

auto cref_tuple = std::make_tuple(std::cref(a), std::cref(b));

Or use a simply as_const helper to qualify the variables before passing them off to std::tie:

template<class T>
T const& as_const(T& v){ return v; }

auto cref_tuple = std::tie(as_const(a), as_const(b));

Or, if you want to get fancy, write your own ctie (reusing std::tie and as_const):

template<class... Ts>
std::tuple<Ts const&...> ctie(Ts&... vs){
  return std::tie(as_const(vs)...);
}

auto cref_tuple = ctie(a, b);
like image 169
Xeo Avatar answered Nov 19 '22 00:11

Xeo


Try forward_as_tuple:

auto test3 = std::forward_as_tuple(ar,br);
like image 44
Howard Hinnant Avatar answered Nov 19 '22 00:11

Howard Hinnant


How about:

auto test3 = std::make_tuple(std::ref(a),std::ref(b));
like image 4
sbk Avatar answered Nov 19 '22 02:11

sbk