Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is no empty argument list required inside this class definition?

When instantiating a variable that has the type of a templated class using only the standard type arguments, the syntax looks like this:

template<typename Arg = int>
class Templ;

Templ<>& myTempl;

Leaving out the empty argument list <> should give a compile error because a template argument list is needed.

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

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

But why does this work? According to IntelliSense, the correct type (Templ<int>) is selected by the compiler, so it works as intended, but shouldn't the member declaration still require an empty argument list?

EDIT: No, it doesn't work as intended. I didn't check it thoroughly enough. When hovering over the line Templ<short>::myTempl, IntelliSense reveals its type to be short.

like image 656
iFreilicht Avatar asked Jul 30 '14 13:07

iFreilicht


People also ask

Why do we use void in the argument list of a function?

Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.

What is meant by function without arguments?

Function with no argument and no return value: When a function has no arguments, it does not receive any data from the calling function. Similarly, when it does not return a value, the calling function does not receive any data from the called function.

What is empty parameter list?

An empty argument list in a function declaration or definition indicates a function that takes no arguments. To explicitly indicate that a function does not take any arguments, you can declare the function in two ways: with an empty parameter list, or with the keyword void : int f(void); int f();

What will happen when we use void in argument list?

What will happen when we use void in argument passing? Explanation: As void is not having any return value, it will not return the value to the caller.


2 Answers

The class name is injected into the class scope

9 Classes [class]

2 A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name. A class-specifier is commonly referred to as a class definition. A class is considered defined after the closing brace of its class-specifier has been seen even though its member functions are in general not yet defined. The optional attribute-specifier-seq appertains to the class; the attributes in the attribute-specifier-seq are thereafter considered attributes of the class whenever it is named.

and similarly for class templates

14.6.1 Locally declared names [temp.local]

1 Like normal (non-template) classes, class templates have an injected-class-name (Clause 9). The injectedclass- 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-typespecifier 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 <>.

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

like image 77
TemplateRex Avatar answered Oct 08 '22 00:10

TemplateRex


This is called the injected class name.

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

like image 40
Jonathan Wakely Avatar answered Oct 07 '22 23:10

Jonathan Wakely