【发布时间】:2011-12-13 14:40:30
【问题描述】:
我一直在尝试将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,using 4.5 doesn't change it)
【问题讨论】:
标签: c++ reference tuples c++11