在显式专业化中的函数名称后的空尖括号

beg*_*ses 4 c++ templates template-specialization

这是文章为什么不专门化功能模板的代码

template<class T>
void f(T);                     // (1)
template<class T>
void f(T*);                   // (2)
template<>
void f<>(int*);             // (3)
Run Code Online (Sandbox Code Playgroud)

我的问题是关于最后的声明。该语法是什么意思?当我们想要完全专门化一个功能模板,例如(1)时,对于某种类型,我们通常会这样写:

template<>
void f<int>(int);
Run Code Online (Sandbox Code Playgroud)

也就是说,我们将该类型放在函数名称后面的尖括号中。

那么语法(3)意味着什么?

Dan*_*ica 6

就你而言

template<> void f<>(int*);
Run Code Online (Sandbox Code Playgroud)

是...的显式专业化

template<class T> void f(T*); 
Run Code Online (Sandbox Code Playgroud)

基本模板。和...一样

template<> void f<int>(int*);
Run Code Online (Sandbox Code Playgroud)

仅推导模板参数。


您甚至可以写:

template<> void f(int*);
Run Code Online (Sandbox Code Playgroud)

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.