std::make_tuple 不进行引用

问题描述 投票:0回答:6

我一直在结合参考文献尝试

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

在此处的三个示例中,前两个按预期工作。然而第三个却没有。我期望

auto
类型 (
test3
) 与
test
的类型相同(即
std::tuple<int&,int&>
)。

似乎

std::make_tuple
无法自动创建引用元组。为什么不?除了我自己明确地构建这种类型的东西之外,我还能做些什么来实现这种情况?

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

c++ reference tuples c++11
6个回答
65
投票

std::tie
保持常量。

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

对于

const
引用,您可能需要
std::cref
包装函数:

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

或者使用简单的

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

或者,如果您想变得更奇特,请编写自己的

ctie
(重用
std::tie
as_const
):

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

auto cref_tuple = ctie(a, b);

39
投票

尝试

forward_as_tuple

auto test3 = std::forward_as_tuple(ar,br);

4
投票

怎么样:

auto test3 = std::make_tuple(std::ref(a),std::ref(b));

3
投票

原因:

make_tuple
参数是通过const引用(
const T&
)传递的,所以如果你传递
int&
T
会匹配
int
。如果它推导
T
int&
,则参数将为
const T&&
,并且您会收到编译错误。


0
投票

在C++14中,你可以这样进行:

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

查看它在 coliru 中的工作原理:http://coliru.stacked-crooked.com/a/a665130e17fd8bcc

干杯 互诫协会


-2
投票

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

诚然,这有点违背了目的,但对于像

make_shared
这样的功能,你仍然可以获得好处。

警告,我还没有尝试编译这个,但我相信它会起作用。

© www.soinside.com 2019 - 2024. All rights reserved.