std :: make_tuple不会引用

Fle*_*exo 36 c++ tuples reference c++11

我一直在尝试std::tuple与参考文献结合:

#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;
}
Run Code Online (Sandbox Code Playgroud)

在这里的三个例子中,前两个按预期工作.然而,第三个没有.我期望autotype(test3)与test(ie std::tuple<int&,int&>)的类型相同.

似乎std::make_tuple无法自动生成引用元组.为什么不?除了明确地自己构建那种类型的东西之外,我能做些什么呢?

(编译器是g ++ 4.4.5,使用4.5不会改变它)

Xeo*_*Xeo 54

std::tie做非const引用.

auto ref_tuple = std::tie(a,b); // decltype(ref_tuple) == std::tuple<int&, int&>
Run Code Online (Sandbox Code Playgroud)

对于const引用,您要么需要std::cref包装函数:

auto cref_tuple = std::make_tuple(std::cref(a), std::cref(b));
Run Code Online (Sandbox Code Playgroud)

或者使用简单的as_const帮助程序来限定变量,然后再将它们传递给std::tie:

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

auto cref_tuple = std::tie(as_const(a), as_const(b));
Run Code Online (Sandbox Code Playgroud)

或者,如果你想得到花哨,写自己的ctie(重用std::tieas_const):

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

auto cref_tuple = ctie(a, b);
Run Code Online (Sandbox Code Playgroud)


How*_*ant 30

试试forward_as_tuple:

auto test3 = std::forward_as_tuple(ar,br);
Run Code Online (Sandbox Code Playgroud)

  • 你可以自己编写4.5及更早版本(只要你有rvalue引用和可变参数支持).这是一个简单的辅助功能.它需要`T && ...`作为参数并返回`tuple <T && ...>(std :: forward <T>(t)...)`. (3认同)