using class name in a class template without template parameters

This is known as the “injected class name.” The rule specifically comes from [temp.local]:

Like normal (non-template) classes, class templates have an injected-class-name (Clause 9). The injected-class-
name
can be used as a template-name or a type-name. When it is used with a template-argument-list,
as a template-argument for a template template-parameter, or as the final identifier in the elaborated-type-specifier
of a friend class template declaration, it refers to the class template itself. Otherwise, it is equivalent
to the template-name followed by the template-parameters of the class template enclosed in <>
.

Within the scope of a class template specialization or partial specialization, when the injected-class-name is
used as a type-name, it is equivalent to the template-name followed by the template-arguments of the class
template specialization or partial specialization enclosed in <>. [ Example:

template<template<class> class T> class A { };
template<class T> class Y;
template<> class Y<int> {
    Y* p;                                // meaning Y<int>
    Y<char>* q;                          // meaning Y<char>
    A<Y>* a;                             // meaning A<::Y>
    class B {
        template<class> friend class Y;  // meaning ::Y
    };
};

—end example ]

This is basically for convenience, so that the class name within the class refers to the class itself and not anything external which may have the same name. For class templates, it potentially saves a lot of typing if you have a long template argument list.

Leave a Comment