#include <iostream>
alignas(16) int i;
int main()
{
std::cout << alignof(i) << std::endl; // output: 16 and warning
std::cout << alignof(decltype(i)) << std::endl; // output: 4
}
What does alignof(i)
and alignof(decltype(i))
do? I would have expected alignof(i)
not to compile and alignof(decltype(i))
to return 16.
CodePudding user response:
alignof(i)
is the alignment of the i
variable, which is explicitly being set to 16, regardless of its type.
alignof(decltype(i))
, aka alignof(int)
, is the natural alignment (sizeof(int)
, ie 4 in this case) for all int
variables that are not otherwise aligned.