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 元组声明?


基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01