【发布时间】:2017-10-15 07:26:53
【问题描述】:
// i.h
template<int> extern int const i;
// i.cpp
#include "i.h"
template<> extern int constexpr i<0> = 42;
// main.cpp
#include "i.h"
int main()
{
return i<0>;
}
在 C++14/17 模式下,这会在 clang 中返回 42,但在 gcc 中会出现错误:“显式模板专业化不能有存储类”。
这是 gcc 中的错误吗?
【问题讨论】:
-
What's the right way to specialize a template when using "extern template"? 的可能重复项 特化是界面的一部分 - 您不能像这样将其隐藏在 .cpp 文件中。例如,特化可能是对某些类型 T 进行 sfinae away 或 static_assert,这需要在客户端代码中知道。
-
不,抱歉,这是一个完全不同的问题。
-
嗯。你能展示最初的模板声明吗?你的意思是在.cpp文件中写contexpr,在.h文件中写const吗?
-
嗯。你想要一个明确的实例化或设置链接吗?请注意,默认情况下全局模板名称具有外部链接...换句话说,您能解释一下最终意图是什么吗?
-
@JohanLundberg 上面的代码是完整的。是的,我的意思是我写的一切。没有错别字。