【发布时间】:2015-10-02 04:32:50
【问题描述】:
见以下代码:
#include <vector>
template class std::vector<int>;
extern template class std::vector<int>;
int main() {}
虽然 GCC 5.2 编译正常,但 clang 3.6 给出以下错误消息:
main.cpp:4:28: error: explicit instantiation declaration (with 'extern') follows explicit instantiation definition (without 'extern')
extern template class std::vector<int>
^
main.cpp:3:21: note: explicit instantiation definition is here
template class std::vector<int>
^
1 error generated.
仍然,对于以下代码
template <typename T>
void f() {}
template void f<int>();
extern template void f<int>();
int main() {}
GCC 和 clang 都出错了。 GCC 的消息是
main.cpp:5:29: error: duplicate explicit instantiation of 'void f() [with T = int]' [-fpermissive]
extern template void f<int>();
而铿锵的那个是
main.cpp:5:22: error: explicit instantiation declaration (with 'extern') follows explicit instantiation definition (without 'extern')
extern template void f<int>();
^
main.cpp:4:15: note: explicit instantiation definition is here
template void f<int>();
^
1 error generated.
这两个家伙怎么了?标准是否禁止显式模板实例化声明前面有显式定义?这对我来说没什么意义。毕竟,先定义再声明又有什么坏处呢?想想非模板函数的情况。
【问题讨论】:
标签: c++ templates c++11 language-lawyer explicit-instantiation