我真正需要的可能是一个静态大小的多维数组
>具有“适当的”价值语义,并且
>连续存储在内存中.
看起来,与C风格的数组不同,std :: array的std :: array不能保证具有完全紧凑的内存,因为std :: array可能包含填充.
如果我使用像std :: array< int [2] [2],2>?这样的问题可能会有什么问题?也许这是一个太模糊的问题,但很难弄清楚为什么我不舒服,有些怀疑使用它为我的目的.
解决方法
容器must be Erasable from the container type的value_type,其中Erasable在[container.requirements.general] paragraph 15中定义:
Given an allocator type
A
and given a container typeX
having avalue_type
identical toT
and anallocator_type
identical toallocator_traits<A>::rebind_alloc<T>
and given an lvaluem
of typeA
,a pointerp
of typeT*
,an expressionv
of type (possiblyconst
)T
,and an rvaluerv
of typeT
,the following terms are defined. IfX
is not allocator-aware,the terms below are defined as if A wereallocator<T>
— no allocator object needs to be created and user specializations ofallocator<T>
are not instantiated:
…
T
is Erasable fromX
means that the following expression is well-formed:
allocator_traits<A>::destroy(m,p)
因为std :: array不支持分配器,所以我们需要检查是否正好形成了allocator_traits< allocator< int [2] [2]>> :: destroy(m,p).
由于std::allocator
没有成员函数destroy(在C 17中弃用),std::allocator_traits::destroy
将直接调用int [2] [2]的(伪)析构函数.这is ill-formed因为int [2] [2]不是scalar type.