使用数组作为元组成员:有效的C ++ 11元组声明? [英] Using array as tuple member: Valid C++11 tuple declaration?

查看:73
本文介绍了使用数组作为元组成员:有效的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 < /元组声明中的/ code>类型,错误消失。上面的元组声明有效吗?

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

推荐答案

我认为标准中没有任何内容禁止您声明。但是,在尝试初始化,复制,移动或分配元组时,您会遇到问题,因为对于这些操作,元组的所有成员类型都必须能够用作初始化程序,可复制构造,可复制分配和可移动分配(第2.0.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元组声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆