Overrides with std :: aligned_storage

The C ++ standard points to a pattern std::aligned_storagethat

Alignmust be equal alignof(T)for some type Tor default alignment.

Does this mean that the program should have such a type or that such a type should be so? In particular, a possible implementation suggested in cppreference,

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 that this makes the type with this alignment, if possible (that is, if it Alignis a valid alignment). Is this behavior required or is this undefined behavior to indicate Alignif such a type does not already exist?

And, perhaps more importantly, is it plausible in practice that the compiler or the standard library could not do the right thing in this case, considering that it Alignis at least legal alignment for the type that has?

+4
source share
1 answer

You can always try to make a type with arbitrary (valid) alignment N:

template <std::size_t N> struct X { alignas(N) char c; };

When Nlarger than the default alignment, Xhas advanced alignment. Support for advanced alignment is determined by the implementation, and [dcl.align] says:

(6.11) , .

, X<N> , , . ( ) X<N>, aligned_storage<Len, N> ( T = X<N>).

aligned_storage - X , X. . aligned_storage , .

+1

Source: https://habr.com/ru/post/1673435/


All Articles