The C++ Standard in 14.2/2 says:
For a template-name to be explicitly qualified by the template arguments, the name must be known to refer to a template.
Can any one Tell this one to prove in terms of programming?
The C++ Standard in 14.2/2 says:
For a template-name to be explicitly qualified by the template arguments, the name must be known to refer to a template.
Can any one Tell this one to prove in terms of programming?
It means that the name must actually be a template when you specify template arguments.
void Foo() {} // A non-template function
class Bar {}; // A non-template class
// These templates accept a single type as a template argument.
template<typename T> void Xyzzy() {} // A template function
template<typename T> class Baz {}; // A template class
int main()
{
// Error, the name "Foo" is not a template!
Foo<char>();
// Error, the name "Bar" is not a template!
Bar<short> bar;
// OK, the name "Xyzzy" is a template, with `int` as a template argument.
Xyzzy<int>();
// OK, the name "Baz" is a template, with `long` as a template argument.
Baz<long> baz;
}
The text is the exact quote of 14.2/2 from the C++ standard. What it means (if that's your question) is explained in greater detail in 14.2/3, 14.2/4 and so on.
What it is basically saying is that the <
symbol can have different meanings in the source code depending on the semantics of the names involved. It can be a less-than operator or it can be an opening angle bracket of a template argument list. So, the quote basically says that when the compiler sees a sequence of tokens like some_name <
, it will determine its meaning based on whether some_name
is a template name or not. To do that the compiler will perform a name lookup on some_name
. If the name lookup reveals that some_name
is declared as a template name, then and only then the <
is interpreted as an opening angle bracket for template argument list. In other words, for this to work, the name lookup must be able to successfully find the previous declaration of the template some_name
in this translation unit. This is what is meant by "must be known to refer to a template".
Also, in situations when the template name is a nested member of a dependent type, the compiler will need user's help in order to determine whether some_name
refers to a template or not. The user must use the keyword template
to tell the compiler that the nested name refers to a template, as shown in 14.2/4.
It means that the compiler must know at the point of usage that the name refers to a template. If the compiler cannot know this, then the name must be qualified with template
as described in 14.2/4.
Example:
struct A
{
template<class X>
static void bar();
};
template<class T>
void f()
{
// This fails to compile!!
T::bar<int>();
}
int main()
{
f<A>();
}
When reading the definition of f()
, the compiler has to decide if the first "<" is to be parsed as a less-than or as the start of a template list. This however depends on, if bar is a simple static member value or a template member function. Since the compiler can not decide this now (without speculative parsing), the standard mandates that you have to use the `template keyword to resolve the ambiguity:
// this works:
T::template bar<int>();
BTW. very old compilers (who implemented templates as a kind of glorious macro) did not need the qualification, but this lead to terrifying compiler errors if something was wrong. So it was decided that the compiler was entitled to some context about what it was supposed to compile.