以下代码声明了一个模板,声明了一个显式的实例化定义,然后声明了一个显式的实例化声明:
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;
}
Run Code Online (Sandbox Code Playgroud)
给出错误:
error C2929: 'int Double<int>(int)' : explicit instantiation; cannot explicitly force and suppress instantiation of template-class member
Run Code Online (Sandbox Code Playgroud)
在Visual Studio 2012中.
我的印象来自http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1987.htm,这应该是有效的,因为定义遵循声明.
我错过了什么吗?