Is the data in nested std::arrays guaranteed to be contiguous?

后端 未结 2 1476
面向向阳花
面向向阳花 2020-11-29 09:30

Is the data in std::array, M> guaranteed to be contiguous? For example:

#include 
#include 

        
2条回答
  •  [愿得一人]
    2020-11-29 10:01

    No, contiguity is not guaranteed in this case.

    std::array is guaranteed to be an aggregate, and is specified in such a way that the underlying array used for storage must be the first data member of the type.

    However, there is no requirement that sizeof(array) == sizeof(T) * N, nor is there any requirement that there are no unnamed padding bytes at the end of the object or that std::array has no data members other than the underlying array storage. (Though, an implementation that included additional data members would be, at best, unusual.)

提交回复
热议问题