Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 868
geza Avatar asked Sep 05 '19 18:09

geza


1 Answers

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

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

    return 0;
}
like image 132
Igor G Avatar answered Nov 05 '22 09:11

Igor G