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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With