How to do it...

  • To control the alignment of a type (both at the class level or data member level) or an object, use the alignas specifier:
        struct alignas(4) foo 
{
char a;
char b;
};
struct bar
{
alignas(2) char a;
alignas(8) int b;
};
alignas(8) int a;
alignas(256) long b[4];
  • To query the alignment of a type, use the alignof operator:
        auto align = alignof(foo);