gpt4 book ai didi

c++ - 为什么const在gcc中影响全局变量模板的实例化的链接?

转载 作者:行者123 更新时间:2023-12-01 14:44:18 25 4
gpt4 key购买 nike

我从两个翻译单元获取以下实例化变量模板的地址:

template<class T> bool b = true;
template<class T> const bool cb = true;
template<class T> inline const bool icb = true;

我正在打印 b<int>cb<int>icb<int>的地址。这是 clang says:
0x6030c0 0x401ae4 0x401ae5  // first translation unit
0x6030c0 0x401ae4 0x401ae5 // second translation unit

所有地址都是相同的,有点像预期的那样。这是 gcc says:
0x6015b0 0x400ef5 0x400ef4  // first translation unit
0x6015b0 0x400ef6 0x400ef4 // second translation unit
cb<int>的地址更改。 ??这是一个错误吗?如果没有,有人可以向我解释这种影响吗?

最佳答案

在我看来,这似乎与CWG Issue 1713有关:

Linkage of variable template specializations

Given a namespace-scope declaration like

template<typename T> T var = T();

should T<const int> have internal linkage by virtue of itsconst-qualified type? Or should it inherit the linkage of thetemplate?

Notes from the February, 2014 meeting:

CWG noted that linkage is by name, and a specialization of a variabletemplate does not have a name separate from that of the variabletemplate, thus the specialization will have the linkage of thetemplate.


Clang似乎正在关注它。模板名称具有外部链接,从中旋转的变量也是如此。
最终,标准本身目前还没有很好地指定变量模板特化的预期链接。它是为常规变量指定的,但是模板是不同的野兽。

关于c++ - 为什么const在gcc中影响全局变量模板的实例化的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58609517/

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