问题描述
下面的代码可以在 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 元组声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!