gpt4 book ai didi

C++ 标准和全局符号删除

转载 作者:行者123 更新时间:2023-11-30 02:13:22 25 4
gpt4 key购买 nike

有人知道 C++ 标准关于编译器删除全局和静态符号的内容的链接吗?我认为您不能保证编译器会删除未被引用的全局符号。我的一位同事断言,如果您的全局符号包含在主翻译单元中,即使没有引用这些符号也不会被删除。

最佳答案

有趣的是,我能在 C++2003 标准中找到的是:

3.7.1 Static storage duration [basic.stc.static]

All objects which neither have dynamic storage duration nor are local have static storage duration. The storage for these objects shall last for the duration of the program (3.6.2, 3.6.3).

If an object of 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 may be eliminated as specified in 12.8.

这意味着如果静态存储中的项目的初始化和销毁​​没有副作用并且它们未被使用,则该标准允许删除它们。

如果有更直接的许可,我没有看到(但也许其他人会看到)。

但是,应该注意的是,让链接器消除最终图像中未使用的对象是一种非常常见的优化。

关于C++ 标准和全局符号删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/430401/

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