compiler error with C++ std::vector of array

后端 未结 3 492
轻奢々
轻奢々 2020-12-21 00:27

the following code doesn\'t compile with gcc 4.7.0 (using std=c++11 -O3)

int n;
std::vector< int[4] > A;
A.resize(n);

the error messa

3条回答
  •  情歌与酒
    2020-12-21 00:47

    See 23.1/3:

    The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types.

    Thus in C++03 vector requires the contained items to be copy constructable, which C-style arrays are not. The error message is correct and the code should fail to compile. Just use a vector of vectors, a struct to wrap your array, or vector of std::array in C++11.

    Note that I believe the copy-constructable restriction is lifted container-wide in C++11 and I'm not sure if/how you could store C-style arrays within one or if it's prohibited more explicitly.

提交回复
热议问题