std::make_tuple doesn#39;t make references(std::make_tuple 不做引用)
问题描述
我一直在尝试将 std::tuple 与参考文献结合使用:
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;
}
在这里的三个示例中,前两个按预期工作.然而第三个没有.我期望 auto 类型 (test3) 与 test 的类型相同(即 std::tuple<int&,int&>).
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&>).
似乎 std::make_tuple 不能自动生成引用的元组.为什么不?除了自己明确构建这种类型的东西之外,我还能做些什么来实现这一点?
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?
(编译器是 g++ 4.4.5,使用 4.5 不会改变它)
(Compiler was g++ 4.4.5, using 4.5 doesn't change it)
推荐答案
试试forward_as_tuple:
auto test3 = std::forward_as_tuple(ar,br);
这篇关于std::make_tuple 不做引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::make_tuple 不做引用
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
