Is it a missed optimization, when a compile-time k

2020-04-07 19:35发布

Note: this is a follow-up question to: Is it a missed optimization, when a compile-time known reference takes space in a struct?, which showed that aggregate initialization can replace the default initialization of b as a reference to a by making it a reference to some other variable. This question is about what happens when aggregate initialization is not a possibility.

See this example:

struct Foo {
    int a;
    int &b;

    Foo() : b(a) { }
};

Is it a missed optimization, if sizeof(Foo)!=sizeof(int)?

I mean, can the compiler remove b from the struct, as it always refers to a?

Is there anything which stops the compiler to make this transformation?

(Note, struct Foo looks as it is. No additional constructors, etc. But you can add anything around Foo, which shows that this optimization would violate the standard)

1条回答
Lonely孤独者°
2楼-- · 2020-04-07 20:07

In the following example y.b refers to x.a.

int main ()
{
    Foo     x;
    Foo     y(x);

    return 0;
}
查看更多
登录 后发表回答