Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal type as a template argument

Tags:

c++

templates

ISO 98/03 standard (section 14.3.1) seems to forbid using a type with internal linkage as a template parameter. (See example below.) The C++11 standard does not. G++ - using the old standard - is allowing it. Am I misreading the 03 standard, or is g++ just letting this slide?

namespace
{
    struct hidden { };
}

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

int main()
{
    S<hidden> s;
    return 0;
}
like image 345
John Avatar asked Nov 03 '11 19:11

John


1 Answers

You're correct that C++03 doesn't allow using a type with internal linkage as a template type parameter, while C++11 does.

I seem to recall, however, that definitions inside the anonymous namespace still have external linkage.


Yup, section 3.5 [basic.link] says

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.

You have a named class at namespace scope, it has external linkage.

And the footnote on the bottom of page 115 of ISO/IEC 14882:2003 clarifies:

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.

If you have another version, try looking in section 7.3.1.1 [namespace.unnamed]

like image 135
Ben Voigt Avatar answered Sep 21 '22 12:09

Ben Voigt