gpt4 book ai didi

c++ - static const 成员是否有内部链接?

转载 作者:搜寻专家 更新时间:2023-10-31 01:42:29 26 4
gpt4 key购买 nike

类的静态成员被编译为类作用域的全局变量。编译的 const 静态成员和静态 constexpr 成员如何?编译器是否为每个 .o 文件制作此静态成员的拷贝或以其他方式完成?

最佳答案

C++14 [class.static.data]/5 涵盖了这一点:

Static data members of a class in namespace scope have external linkage. A local class shall not have static data members.

“命名空间范围内的类”表示不在 block 范围内的类(又名“本地类”)。例如这段代码:

void func()
{
class C { static const int x = 5; };
}

格式错误。


回答你的问题:

Does compiler for every .o file makes copy of this static member or it's done otherwise?

通常,如果静态成员在类之外有一个定义,那么在目标文件中对应于该定义的位置将有一个拷贝,否则将没有。

关于c++ - static const 成员是否有内部链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26777821/

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