Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
149 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...