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 y.b
refers to x.a
.
int main ()
{
Foo x;
Foo y(x);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With