The Mask
The Mask

Reputation: 17427

What's the behavior of sizeof used in member initializer?

What's the behavior of sizeof used in member initializer? I can't find a description about exactly that. In the below code example is it safe? ie, after struct member will the compiler put right size of A in b?

struct A
{
    int a = 0;
    int b = sizeof(A);
    int c = 3;
    int d = 4;
    char s[256];

    A()
    {
        //printf("b = %d\n", b);
    }
};

Upvotes: 2

Views: 130

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129374

The size of a struct is determined much earlier in the compilation than the setting of the values inside the struct.

Upvotes: 4

Related Questions