特定
#include <vector>
// Case I: error
Run Code Online (Sandbox Code Playgroud)
错误C2961:'std :: vector>':不一致的显式实例化,之前的显式实例化没有指定'extern template'
template class std::vector<int>;
extern template class std::vector<int>;
// Case II: fine
//extern template class std::vector<int>;
//template class std::vector<int>;
// Case III: fine
//extern template class std::vector<int>;
//template class std::vector<int>;
//template class std::vector<int>;
// Case IV: fine
//extern template class std::vector<int>;
//extern template class std::vector<int>;
//template class std::vector<int>;
//template class std::vector<int>;
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
我使用VS2015版本4.6构建代码并查看C2961错误.但是,我没有看到编译器http://gcc.godbolt.org/的错误.
这是VS2015的错误吗?
MSVC和Clang在这种情况下发出错误是正确的.标准中的[temp.explicit]/11表示:
如果实体是同一翻译单元中的显式实例化声明和显式实例化定义的主题,则该定义应遵循声明.[...]
template class std::vector<int>;是一个显式的实例化定义.带有的extern是一个显式的实例化声明.
请注意,std::vector<int>根据[namespace.std]/2 ,显式实例化在任何情况下都是格式错误的:
[...]只有当声明取决于用户定义类型的名称并且实例化符合原始模板的标准库要求时,程序才可以显式实例化标准库中定义的模板.
另请注意,根据[temp.spec]/5,案例III和IV也是不正确的:
对于给定的模板和给定的模板参数集,
- 显式实例化定义在程序中最多只出现一次,
[...]
诊断违反此规则不需要实现.
| 归档时间: |
|
| 查看次数: |
315 次 |
| 最近记录: |