45

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)

Flexo
  • 87,323
  • 22
  • 191
  • 272

6 Answers6

65

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);
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • It's worth emphasising that `std::[c]ref()` on `tuple` arguments can be mixed with arguments lacking it, to deduce a mixture of value and reference arguments. Also: C++17 can deduce template arguments from constructor expressions and so can use `std::tuple{...}` instead of `std::make_tuple(...)`, and it added an official `std::as_const()`. Anyway, good point about `std::tie()`; I tend to forget it exists, and it's far less noisy than `std::forward_as_tuple()`. – underscore_d Nov 15 '18 at 14:36
  • "`std::tie` makes non-`const` references." This statement is somewhat misleading. `std::tie` creates a tuple of lvalue references to its arguments. Whether these lvalue references refer to `const`-qualified types (aka `const` lvalue references) depends on the type being deduced. If `a` and `b` are declared `const`, then `std::tie` makes `const` lvalue references. – 303 Jul 09 '21 at 23:12
37

Try forward_as_tuple:

auto test3 = std::forward_as_tuple(ar,br);
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • @Dani: Why can't `tuple` (the return type of `std::forward_as_tuple` as used above) be stored? Fwiw, I tested the code before I posted it. – Howard Hinnant Oct 23 '11 at 16:13
  • It compiles and works as expected with g++ 4.7.0 (10/10/2011 snapshot) and 4.6.1, but not with any 4.5 or earlier that I tried – Flexo Oct 23 '11 at 16:15
  • 4
    You could write your own for use with 4.5 and earlier (as long as you have rvalue reference and variadic support). It is a trivial helper function. It takes `T&&...` as its argument and returns `tuple(std::forward(t)...)`. – Howard Hinnant Oct 23 '11 at 16:22
4

How about:

auto test3 = std::make_tuple(std::ref(a),std::ref(b));
sbk
  • 9,212
  • 4
  • 32
  • 40
  • where does `std::ref` come from? With g++ 4.7 I'm getting "ref is not a member of std", and grep in monumentally unhelpful trying to work that one out. – Flexo Oct 23 '11 at 16:23
  • 1
    Should be in ``. On MSVC2010 it seems to be dragged in by `` – sbk Oct 23 '11 at 16:39
  • I like this answer because I think it makes something like `std::make_tuple(p1?std::ref(a):std::cref(a), p2?std::ref(b):std::cref(b))` feasible – Flexo Oct 24 '11 at 08:45
3

For the why: make_tuple parameters are passed by const reference (const T&), so if you pass int&, T matches int. If it deduced T to be int&, the parameter would be const T&&, and you'd get a compile error.

Dennis Zickefoose
  • 10,791
  • 3
  • 29
  • 38
  • The reference collapsing rules are that `T = int&` applying to `const T&` results in `const int&` – M.M Mar 11 '18 at 03:14
0

In C++14, you can proceed like that:

template<typename ...T, size_t... I>
auto make_rtuple_helper(std::tuple<T...>& t ,  std::index_sequence<I...>)
-> std::tuple<T&...>
{ return std::tie(std::get<I>(t)...) ;}

template<typename ...T>
std::tuple<T&...> make_rtuple( std::tuple<T...>& t )
{
    return make_rtuple_helper( t, std::make_index_sequence<sizeof...(T)>{});
}

See the how it works here in coliru : http://coliru.stacked-crooked.com/a/a665130e17fd8bcc

Cheers A.A.

-2

What about std::make_tuple<int&, int&>(a, b);

Admittedly, it kind of defeats the purpose, but for functions like make_shared you still get benefits.

Warning, I haven't tried to compile this, but I believe it will work.

Michael Price
  • 8,088
  • 1
  • 17
  • 24
  • The idea was to avoid ever specifying the types explicitly because it's a lot more cumbersome to do in the real code – Flexo Oct 23 '11 at 16:05
  • @awoodland Yeah, my answer kind of sucks. But I didn't know about std::forward_as_tuple before. That looks like the correct answer to me. – Michael Price Oct 23 '11 at 16:44
  • `make_tuple()` exists to deduce the types. If you could/wanted to specify them explicitly, there would be no point using `make_tuple`, and you should just invoke the constructor directly. – underscore_d Oct 21 '18 at 19:50