Using array as tuple member: Valid C++11 tuple declaration?(使用数组作为元组成员:有效的 C++11 元组声明?)
问题描述
下面的代码可以在 G++ 4.7.2 中正常编译:
The code below compiles fine with G++ 4.7.2:
#include <tuple>
std::tuple<float,int[2]> x;
但是,使用 clang++ 3.2,会产生以下错误:
With clang++ 3.2, however, the following error is produced:
错误:数组初始化器必须是初始化器列表.
如果我从元组声明中删除 float 类型,错误就会消失.上面的元组声明有效吗?
If I remove the float type from the tuple declaration, the error disappears. Is the above tuple declaration valid?
($CXX -std=c++11 -c file.cpp)
推荐答案
我认为标准中没有任何内容禁止您声明.但是,一旦尝试初始化、复制、移动或分配元组,您就会遇到问题,因为对于这些操作,元组的所有成员类型都必须能够用作初始化器、可复制构造、可复制分配和可移动分配,分别(§20.4.2.1).数组都不是这种情况.
I don't think there is anything in the Standard that forbids your declaration. However, you will run into problems as soon as you try to initialise, copy, move or assign your tuples, because for these operations, all member types of the tuple must be able to be used as initialisers, copy-constructible, copy-assignable and move-assignable, respectively (§20.4.2.1). None of this is the case for arrays.
最好使用 std::array 而不是 C 风格的数组:
You will be better off using std::array instead of C-style arrays:
#include <tuple>
#include <array>
std::tuple<float,std::array<int,2> > x;
这篇关于使用数组作为元组成员:有效的 C++11 元组声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用数组作为元组成员:有效的 C++11 元组声明?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
