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

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

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

#include 
#include 

        
2条回答
  •  心在旅途
    2020-11-29 09:51

    They are very likely contiguous. If they are not, the compiler is actively fighting you there. There's no guarantee it won't insert padding but there's hardly a reason for it.

    Is the assert guaranteed to succeed?

    data[7] is an out-of-bounds access (undefined behaviour). The inner array object has only seven elements, so index 7 is not valid.

提交回复
热议问题