VS 2012中的显式模板声明/定义 [英] Explicit template declaration/definition in VS 2012

查看:485
本文介绍了VS 2012中的显式模板声明/定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following code declares a template, declares an explicit instantiation definition, then declares an explicit instantiation declaration:

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

的实例化。

我受到 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1987.htm 这应该是有效的,因为定义在声明之后。

I'm under the impression from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1987.htm that this should be valid, since the definition follows the declaration.

我缺少一些东西?

推荐答案

C ++ 11标准的第14.7.2 / 11段规定:

Your program is well-formed. Paragraph 14.7.2/11 of the C++11 Standard specifies:


如果实体是明确的实例化声明和在同一翻译单元中的显式实例化定义
,定义将遵循声明。 [...]

If an entity is the subject of both an explicit instantiation declaration and an explicit instantiation definition in the same translation unit, the definition shall follow the declaration. [...]

您的程序遵守此约束,不会破坏任何其他规则。因此,这是VC11中的一个错误。

Your program respects this constraint and does not break any other rule. Therefore, this qualifies as a bug in VC11.

这篇关于VS 2012中的显式模板声明/定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆