VS 2012中的显式模板声明/定义

Jef*_*tes 8 c++ templates c++11 visual-studio-2012

以下代码声明了一个模板,声明了一个显式的实例化定义,然后声明了一个显式的实例化声明:

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,这应该是有效的,因为定义遵循声明.

我错过了什么吗?

And*_*owl 7

您的计划格式正确.C++ 11标准的第14.7.2/11段规定:

如果实体是同一翻译单元中的显式实例化声明和显式实例化定义的主题,则该定义应遵循声明.[...]

您的程序遵守此约束,并且不会破坏任何其他规则.因此,这符合VC11中的错误.