JohnFreeman1212
JohnFreeman1212

Reputation: 145

What is the difference between alignof(i) and alignof(decltype(i))?

#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.

Upvotes: 2

Views: 474

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597166

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.

Upvotes: 2

Related Questions