微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 内部类型作为模板参数

ISO 98/03标准(第14.3.1节)似乎禁止使用具有内部链接的类型作为模板参数. (参见下面的示例.)C 11标准没有.
G – 使用旧标准 – 允许它.
我是在误读03标准,还是只是让这张幻灯片
namespace
{
    struct hidden { };
}

template<typename T>
struct S
{
   T t;
};

int main()
{
    S<hidden> s;
    return 0;
}

解决方法

你是正确的,C 03不允许使用具有内部链接的类型作为模板类型参数,而C 11则允许.

然而,我似乎记得,匿名命名空间内的定义仍然具有外部链接.

是的,第3.5节[basic.link]说

A name having namespace scope (3.3.5) has internal linkage if it is the name of

  • an object,reference,function or function template that is explicitly declared static or,
  • an object or reference that is explicitly declared const and neither explicitly declared extern nor prevIoUsly declared to have external linkage; or
  • a data member of an anonymous union.

A name having namespace scope has external linkage if it is the name of

  • an object or reference,unless it has internal linkage; or
  • a function,unless it has internal linkage; or
  • a named class (clause 9),or an unnamed class defined in a typedef declaration in which the class has the typedef name for linkage purposes (7.1.3); or
  • a named enumeration (7.2),or an unnamed enumeration defined in a typedef declaration in which the enumeration has the typedef name for linkage purposes (7.1.3); or
  • an enumerator belonging to an enumeration with external linkage; or
  • a template,unless it is a function template that has internal linkage (clause 14); or
  • a namespace (7.3),unless it is declared within an unnamed namespace.

您在命名空间范围内有一个命名类,它具有外部链接.

ISO / IEC 14882:2003第115页底部的脚注阐明:

Although entities in an unnamed namespace might have external linkage,they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.

如果您有其他版本,请尝试查看第7.3.1.1节[namespace.unnamed]

原文地址:https://www.jb51.cc/c/111549.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐