ISO 98/03标准(第14.3.1节)似乎禁止使用具有内部链接的类型作为模板参数. (参见下面的示例.)C 11标准没有.
G – 使用旧标准 – 允许它.
我是在误读03标准,还是只是让这张幻灯片?
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 declaredextern
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]