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
Aand given a container typeXhaving avalue_typeidentical toTand anallocator_typeidentical toallocator_traits<A>::rebind_alloc<T>and given an lvaluemof typeA, a pointerpof typeT*, an expressionvof type (possiblyconst)T, and an rvaluervof typeT, the following terms are defined. IfXis 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:
...
Tis Erasable fromXmeans 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