简体   繁体   中英

Is it a missed optimization, when a compile-time known reference takes space in a non-aggregate struct?

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)

In the following example yb refers to xa .

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

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM