当编译时已知的引用占用结构中的空间时,是否错过了优化? [英] Is it a missed optimization, when a compile-time known reference takes space in a struct?

查看:45
本文介绍了当编译时已知的引用占用结构中的空间时,是否错过了优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见以下示例:

struct Foo {
    int a;
    int &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 constructors, etc. But you can add anything around Foo, which shows that this optimization would violate the standard)

推荐答案

否,因为您可以使用变量的聚合初始化使它引用其他变量.

No, because you can use aggregate initialization of a variable to have it refer to something else.

struct Foo {
    int a;
    int &b = a;
};

int c;
Foo f{7, c};

这篇关于当编译时已知的引用占用结构中的空间时,是否错过了优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆