Using array as tuple member: Valid C++11 tuple declaration?

北战南征 提交于 2019-12-31 01:05:25

问题


The code below compiles fine with G++ 4.7.2:

#include <tuple>
std::tuple<float,int[2]> x;

With clang++ 3.2, however, the following error is produced:

error: array initializer must be an initializer list.

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)


回答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.

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;


来源:https://stackoverflow.com/questions/14804039/using-array-as-tuple-member-valid-c11-tuple-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!