c++alignment

what can be overaligned types


is "Tis overaligned" equivalent to alignof(T)>alignof(std::max_align_t)? And is it possible to have an overaligned type without using alignas keyword or another explicit alignment mechanism (in which case, could an example be provided)?


Solution

  • "T is overaligned" is actually equivalent to alignof(T)>alignof(std::max_align_t): for instance in C++20 (emphasis mine):

    An extended alignment is represented by an alignment greater than alignof(std​::​max_­align_­t). It is implementation-defined whether any extended alignments are supported and the contexts in which they are supported ([dcl.align]). A type having an extended alignment requirement is an over-aligned type.

    https://timsong-cpp.github.io/cppwp/n4861/basic.align#3

    This is an example of overaligned type without explicit alignment mechanism, using non standard type (from AVX):

    #include <immintrin.h>
    #include <iostream>
    
    int main() {
        std::cout << alignof(std::max_align_t);
        std::cout << '\t';
        std::cout << alignof(__m256); // __m256 is an AVX type
    }
    

    LIVE

    Output varies with the hardware and, sometimes, alignof(__m256) is strictly greater than alignof(std::max_align_t).

    I couldn't think of a standard-only example.