gez*_*eza 27 c++ struct initialization reference language-lawyer
请参阅以下示例:
struct Foo {
int a;
int &b = a;
};
Run Code Online (Sandbox Code Playgroud)
如果错过了优化sizeof(Foo)!=sizeof(int)吗?
我的意思是,编译器可以b从其始终引用的结构中删除它a吗?
有什么阻止编译器进行这种转换的吗?
(请注意,struct Foo看起来是这样。没有构造函数,等等。但是您可以在周围添加任何内容Foo,这表明该优化将违反标准。)
120*_*arm 41
不可以,因为您可以使用变量的聚合初始化来使其引用其他对象。
struct Foo {
int a;
int &b = a;
};
int c;
Foo f{7, c};
Run Code Online (Sandbox Code Playgroud)