Why is there no empty argument list in this class definition?

When creating an instance of a variable of the template class type using only standard type arguments, the syntax is as follows:

template<typename Arg = int> class Templ; Templ<>& myTempl; 

Leaving an empty argument list <> should give a compilation error, because a list of template arguments is needed.

But apparently (at least under VS2013), the following declaration does not need a list of template arguments:

 template<typename Arg> //" = int" left out class Templ{ Templ& myTempl; //no <> here }; 

But why does this work? According to IntelliSense, the selected type ( Templ<int> ) is selected by the compiler, so it works as intended, but should not declare the member to use an empty argument list?

EDITOR: No, it is not working properly. I did not check it carefully enough. When you hover over the line Templ<short>::myTempl IntelliSense shows that its type is short .

+6
source share
2 answers

The class name is entered in the class scope

9 Classes [Class]

2 The class name is inserted into the area in which it is declared immediately after viewing the class name. The class name is also inserted in the scope of the class itself; This is known as the injectable class name . For access control purposes, inted-class-name is treated as the name of a public member. A class specifier is usually called a class definition. A class is defined after closing the brackets of its class specifier has been noticed, although its member functions as a whole have not yet been defined. The optional attribute specifier seq refers to the class; after that, the attributes in the specifier attribute-seq read the attributes of the class whenever it is named.

and similarly for class templates

14.6.1 Locally Declared Names [temp.local]

1 Like regular (non-template) classes, class templates have an inted-class-name (section 9). The class name you enter can be used as a template name or type name. When used with template-argument-list, as template-argument for template template-parameter, or as the final identifier in the developed-typespecifier of a friend’s class template declaration, it refers to the class template itself. Otherwise, this is equivalent to the name of the template, followed by the template parameters of the template class enclosed in <> .

so that you can use Templ where you mean Templ<Arg> .

+10
source

This is called the entered class name.

Inside the class template, the template name without a list of template templates refers to the current instance, so Templ means Templ<Arg> (not Templ<> , which is Templ<int> and therefore not necessarily the same).

+8
source

Source: https://habr.com/ru/post/973102/


All Articles