小编Jef*_*tes的帖子

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

我错过了什么吗?

c++ templates c++11 visual-studio-2012

8
推荐指数
1
解决办法
1025
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

visual-studio-2012 ×1