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]

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...