聚合类中默认初始化const引用的生存期延长问题
关于const引用绑定临时对象生存期延长的疑问
我了解将prvalue绑定到const引用变量会延长临时对象的生存期,因此设计了一个options结构体,其中包含const B_options&类型的成员b_cfg(用于在我的场景中初始化子类),这样复制该对象时仅占用一个引用的空间,无需存储整个子类配置。这段代码在GCC中编译运行完全正常:
#include <cstdio> struct B_options { int a = 10; }; struct options { const B_options& b_cfg = {}; }; int main() { options cfg = {}; printf("b_cfg = %d", cfg.b_cfg.a); }
出于好奇我尝试用Clang trunk编译,却出现如下警告:
<source>:15:20: warning: sorry, lifetime extension of temporary created by aggregate initialization using default member initializer is not supported; lifetime of temporary will end at the end of the full-expression [-Wdangling] options cfg = {}; ^ <source>:10:22: note: initializing field 'b_cfg' with default member initializer const B_options& b_cfg = {};
请问这属于未定义行为(UB)还是Clang暂未实现该特性?我使用GCC,但不愿踏入UB的范畴。
内容的提问来源于stack exchange,提问作者glades
相关产品推荐
相关产品推荐

