Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty angle-brackets after a function's name in explicit specialization

Here is a code from the article Why not specialize function templates?

template<class T>
void f(T);                     // (1)
template<class T>
void f(T*);                   // (2)
template<>
void f<>(int*);             // (3)

My question is about the last declaration. What does that syntax mean? When we want to fully specialize a function template, e.g. (1), for some type we usually write:

template<>
void f<int>(int);

i.e. we put that type into the angle-brackets after the function' name.

So what does the syntax (3) means?

like image 420
beginpluses Avatar asked Jan 27 '23 03:01

beginpluses


2 Answers

In your case,

template<> void f<>(int*);

is an explicit specialization of

template<class T> void f(T*); 

base template. It's the same as

template<> void f<int>(int*);

just the template argument is deduced.


You can even write:

template<> void f(int*);

with the same effect. A similar case is presented on cppreference, see section Explicit specializations of function templates, where there is written:

When specializing a function template, its template arguments can be omitted if template argument deduction can provide them from the function arguments.

The relevant part of the C++ Standard: http://eel.is/c++draft/temp.expl.spec#11.

like image 103
Daniel Langr Avatar answered Feb 19 '23 10:02

Daniel Langr


A missing template argument will be deduced by the compiler if you don't provide it. In this particular case it will be deduced to int* because the second template is more specialized (but both are the candidates). So

template<> void f<>(int*)

will become

template<> void f<int>(int*)

after type deduction.

like image 28
Evg Avatar answered Feb 19 '23 09:02

Evg