gpt4 book ai didi

c++ - 未使用的全局变量的静态初始值设定项

转载 作者:太空狗 更新时间:2023-10-29 19:56:43 26 4
gpt4 key购买 nike

如果我有一个从未使用过的具有内部链接的全局,它的初始化程序是否保证运行?例如:

static int x = SideEffectfulFunction();

namespace {
int x = SideEffectfulFunction();
}

SideEffectfulFunction() 是否保证被调用,即使 x 从未被引用?还是删除 x 是合法的?

最佳答案

是的,标准保证了这一点

静态存储时长[basic.stc.static]

2 If a variable with static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy/move may be eliminated as specified in 15.8.

关于c++ - 未使用的全局变量的静态初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44753653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com