加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:23:38 所属栏目:百科 来源:网络整理
导读:ISO 98/03标准(第14.3.1节)似乎禁止使用具有内部链接的类型作为模板参数. (参见下面的示例.)C 11标准没有. G – 使用旧标准 – 允许它. 我是在误读03标准,还是只是让这张幻灯片? namespace{ struct hidden { };}templatetypename Tstruct S{ T t;};int main
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]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读