Is it okay to use std::array<std::byte, N> storage as storage for allocation of some memory?
For example, is it safe to call placement-new on a.data(), even though the lifetime of the allocated object has nothing to do with std::array?
std::aligned_storage_t was deprecated in C++23 for a reason, that it has UB rooted in its design (and poor API but it's not the case in this question), while it's pretty much close to this use case of std::array, so is it mandatory to use C-style array std::byte storage[N] as a storage for memory allocation, not std::array (or maybe even std::vector).
The issue with
std::aligned_storage_tis that when you start the lifetime of the new object, the lifetime of thestd::aligned_storage_tobject ends. [basic.life]p1:So if you were to use the
std::arrayas if it werestd::aligned_storage_t, you would not be able to call any of the member functions of the array since the arrays lifetime would end:However, since you use the
.data()pointer ofstd::array, then the lifetime would not end because the memberstd::byte[N]would provide storage for the new object [intro.object]p3:And by [intro.object]p4:
So instantiate the second option with b = the member
std::byte[N]ofarrand c = the newly constructed object, and the third option with c = the memberstd::byte[N]and a = the newly constructed object and b = thestd::array<std::byte, N>object. The newly constructed object is nested within thestd::array.So it can be used in the same way as if the object were
std::byte[N]instead ofstd::array<std::byte, N>(namely to provide storage for a new object that is nested in the array/std::array):