以下代码声明一个模板,声明一个显式实例化定义,然后声明一个显式实例化声明:
template <typename T>
T Double(T number)
{
return number * 2;
}
extern template int Double<int>(int); // declaration
template int Double<int>(int t); // definition
int main(int argc, char* argv[])
{
int n = Double(10);
return 0;
}
给出错误:
error C2929: 'int Double<int>(int)' : explicit instantiation; cannot explicitly force and suppress instantiation of template-class member
在 Visual Studio 2012 中。
我的印象是http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1987.htm http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1987.htm这应该是有效的,因为定义遵循声明。
我错过了什么吗?
您的程序格式良好。 C++11 标准第 14.7.2/11 段规定:
如果实体既是显式实例化声明又是显式实例化定义的主题
在同一翻译单元中,定义应遵循声明。 [...]
您的程序尊重此约束并且不违反任何其他规则。因此,这属于 VC11 中的错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)