11
votes

It would be nice if this code were invalid. But it's conceptually sound, and GCC accepts it although Comeau doesn't:

template< typename > struct t;

template<> struct t< int > {} r; // Bad declarator! Don't pee on the carpet!

(Edit: the above compiles but r seems no to be declared into any scope, so it is essentially ignored.)

Explicit specializations populate a kind of nether region between templates and classes. The type declared by an explicit specialization is complete once it is defined. From the compiler's standpoint, it is not a template. If it were a parameterized template, declaring an object would be impossible. Consider §14/3:

In a template-declaration, explicit specialization, or explicit instantiation the init-declarator-list in the dec- laration shall contain at most one declarator. When such a declaration is used to declare a class template, no declarator is permitted.

What does "is used to declare a class template" mean? Clearly a primary template declares a class template. And a partial specialization does too, according to §14.5.5/1 (FDIS numbers):

A template declaration in which the class template name is a simple-template-id is a partial specialization of the class template named in the simple-template-id.

When it comes to explicit specializations, though, the Standard speaks in terms of a declaration preceded by the token sequence template<>. It looks like a template and it names a template-name, but it doesn't seem to declare a template.

The really bizarre thing is that §14/3 restricts the number of declarators to "at most one." A function template declaration, explicit specialization or instantiation must have exactly one declarator. Any declaration involving a class template must have exactly zero… except explicit specialization, which seems to fall through the cracks. Faithfully, GCC refuses to allow

template<> struct t< int > {} r, s; // Offer valid one per specialization.

I tend to agree with GCC's interpretation, nonsense as it may be. Unfortunately, it may be inhibiting its ability to detect missing semicolons. Please, let the number of allowed declarators be exactly zero!

2
Consider template<typename T> template<typename U> struct A { struct B { }; }; template<> template<typename T> struct A<int>::A { };. Here, an explicit specialization happens for a member template of the instantiation of A<int>, but without specializing the member template itself (there is not a real term for this kind of explicit specializations. At one point, the spec uses the terms "specialized as a template" once when it refers to such cases: "However, template<> is used in defining a member of an explicitly specialized member class template that is specialized as a class template.").Johannes Schaub - litb
Out of curiosity, I tried compiling it with clang++. It prints an error "extraneous 'template<>' in declaration of variable 'r'". Obviously, when tempalte<> is ommited, it gets an error "template specialization requires 'template<>'".CygnusX1

2 Answers

13
votes

Several points: first, explicit specializations are not in a nether region between templates and classes; an explicit specialization is a class, period. The only relation is has with templates (except for the funny name) is that it will be used instead of a template instantiation if the template is to be instantiated on the specialization type.

Secondly, if there is a problem with the paragraph in §14/3 that you cite, it is that it includes explicit instantiation; an explicit instantiation is a class definition, and if

struct S {} s, *p;

is legal,

template<> struct T<int> {} s, *p;

should be too. (I would argue against allowing either, but that train has already left the station, and since C allows the first, we're stuck with it.)

Otherwise, the statement in §14/3 is a bit irrelevant. A function template must have exactly one declarator, and a class template exactly zero; there's no need to try to englobe them both in some "at most one" gobbledygook. (If I were designing the language from scratch, I'd not allow any declarator in a declaration which defined a class or enum type. But again, it's too late for that.)

And I agree that it's a bother:

template<> struct T<int> {};    //  Requires a ';'
template<> void f<int>() {}     //  ';' forbidden

(At least C++11 will allow a semicolon after the function definition.)

1
votes

Explicit specialization and explicit instantiation do not declare a template. They declare a template-id which refers to a specialization, which is a class.

However, this doesn't validate my example. The problem is that everything declared following template or template<> is part of the explicit instantiation or specialization, respectively. Only certain types of entities may be specialized or instantiated, and previously-undeclared names aren't one of them.

Consider these examples making gratuitous, but legal use of elaborated-type-specifiers (§7.1.5.3):

template< typename T > struct s;
template< typename T > s< int > *f() {}

template<> struct u *f< char >(); // struct u is declared
u *p = 0; // see, we can use its name now.
template<> struct s< int > *f< int >(); // s<int> declared but not specialized
template struct s< int > *f< long >(); // s<int> declared but not instantiated

As far as I can tell, the Standard is fuzzy about specifying which declared name is the one specialized. The language does weakly imply that each such declaration applies to only one template: §14.7.2/2

If the explicit instantiation is for a class, a function or a member template specialization…

and §14.7.3/2

An explicit specialization shall be declared in the namespace of which the template is a member…

The only way to resolve this is to ignore the type declaration if the declarator also specifies a legal instantiation/specialization.

Getting to the point, the examples in the question specify illegal specializations in the declarator and then expect the compiler to backtrack and specialize the type instead. Given the explicit lists of what specializations and declarations are allowed to do in §14.7.2/1 and §14.7.3/1, it seems more reasonable to complain about template<> struct t< int > {} r; that r is not a function template, member function template, static data member of a class template, etc.