I am mainly referring to the C++03 standard but, after a quick glance, it should also be applicable to the C++11 standard.
The following code compiled and executed successfully in VC++2010:
template<typename T>
class CC {
public:
T f(T a) {
return a*a;
}
};
template<>
class ::CC<int> { //<--- ::CC<int> syntax allowed by VC++2010, but is it non-standard ?
public:
int f(int a) {
return a*a;
}
};
int main(int argc, _TCHAR* argv[])
{
::CC<int> c;
}
Notice the ::CC<int>
syntax to refer to the template defined in the global namespace. This is not the same as the NamespaceA::CC<int>
syntax where the ::
operator is preceded by something. With some other tools, I tried to parse this using the grammar strictly from the C++03 but it gave me errors and it seems to me that the standard accepts only NamespaceA::CC<int>
form in the class head declaration.
On a closer look, the issue is that the class-head
is defined by this grammar in the standard:
class-head:
class-key identifier(optional) base-clause(optional)
class-key nested-name-specifier identifier base-clause(optional)
class-key nested-name-specifier(optional) template-id base-clause(optional)
And since nested-name-specifier
is of the form AA::bb::
..., it doesn't accept my ::CC
.
My question is, why the C++ standard doesn't allow the ::CC form? Is it just my incorrect interpretation of the standard grammar? Should the proper grammar looks like this:
class-head:
...
class-key '::'(optional) nested-name-specifier(optional) template-id base-clause(optional)
Note, the above form is really used by the standard somewhere else, say, in specifying declarator-id:
declarator-id:
id-expression
::(optional) nested-name-specifier(optional) class-name