gpt4 book ai didi

c++ - 显式特化中函数名称后的空尖括号

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:21:37 26 4
gpt4 key购买 nike

这是文章 Why not specialize function templates? 中的代码

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

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

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

即我们将该类型放入函数名称后的尖括号中。

那么语法(3)是什么意思呢?

最佳答案

在你的情况下,

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

的显式特化
template<class T> void f(T*); 

基本模板。这是一样的

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

仅推导出模板参数。


你甚至可以这样写:

template<> void f(int*);

效果相同。 cppreference 上有类似案例,请参阅函数模板的显式特化部分,其中写道:

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

C++ 标准的相关部分:http://eel.is/c++draft/temp.expl.spec#11 .

关于c++ - 显式特化中函数名称后的空尖括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56771691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com