The C++ standard states, regarding the std::aligned_storage template, that
Alignshall be equal toalignof(T)for some typeTor to default-alignment.
Does that mean that there must be such a type in the program, or that it must be possible to make such a type? In particular, the possible implementation suggested on cppreference is
template<std::size_t Len, std::size_t Align /* default alignment not implemented */>
struct aligned_storage {
typedef struct {
alignas(Align) unsigned char data[Len];
} type;
};
It seems like this makes a type with that alignment, if possible (that is, if Align is a valid alignment). Is that behavior required, or is it undefined behavior to specify an Align if such a type does not already exist?
And, perhaps more importantly, is it plausible in practice that the compiler or standard library would fail to do the right thing in this case, assuming that Align is at least a legal alignment for a type to have?
Tis the type of the hypothetical object you want to store, whose alignment should match or evenly divideAlignand whose size is at leastLen. Are you asking what happens if you were to specify a strange value forAlign? (like 13)std::aligned_storageor if you manually need to create the storage with the over-alignment. That’s what I’m asking about.std::aligned_storagecan be used to over-align objects.Ts alignment doesn't have to matchAlignit just has to be a divisor.Tis not necessarily the same as the type you want to put in the storage. This says that if I want to use a 4096-bytestd::aligned_storagethere must be a 4096-byte aligned data type. I know that if I have a 4096-byte aligned storage I can use it for anything which requires an 8-byte alignment (or any other factor of 4096), but that doesn’t tell me if I can construct such a storage in the first place.