当编译时已知的引用在非聚合结构中占用空间时,是否错过了优化? [英] 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.
请参见以下示例:
struct Foo {
int a;
int &b;
Foo() : b(a) { }
};
如果sizeof(Foo)!=sizeof(int)
,是否错过了优化?
Is it a missed optimization, if sizeof(Foo)!=sizeof(int)
?
我的意思是,编译器是否可以从结构中删除b
,因为它始终引用a
?
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?
(请注意,struct Foo
外观保持不变.没有其他构造函数,等等.但是您可以在Foo
周围添加任何内容,这表明此优化将违反标准)
(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)
推荐答案
在以下示例中,y.b
引用了x.a
.
In the following example y.b
refers to x.a
.
int main ()
{
Foo x;
Foo y(x);
return 0;
}
这篇关于当编译时已知的引用在非聚合结构中占用空间时,是否错过了优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!