Can I use something like std::array<int[2][2], 2>
as a substitute for int[2][2][2]
, just like std::array<int, 2>
can be used instead of int[2]
?
What I really need is maybe a statically-sized multidimensional array that
It seems that, unlike C-style arrays, std::array
of std::array
is not guaranteed to have fully compactified memory, as std::array
may contain padding.
What are possible issues I might have if I use something like std::array<int[2][2], 2>
? Perhaps this is a too vague question, but it's hard to figure out why exactly I'm not comfortable and somewhat doubtful using it for my purpose.
No, it results in undefined behavior.
The value_type
of a container must be Erasable from the container type, where Erasable is defined in [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)
Because std::array
is not allocator-aware, we need to check if allocator_traits<allocator<int[2][2]>>::destroy(m, p)
is welll formed.
Since std::allocator
has no member function destroy
(deprecated in C++17), std::allocator_traits::destroy
would call the (pseudo) destrutor of int[2][2]
directly. This is ill-formed because int[2][2]
is not a scalar type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With