根据模板函数直接定义非模板函数

Directly defining a non-template function in terms of a template function

本文关键字:函数 定义      更新时间:2023-10-16

我可以给模板函数的特定实例一个名称,这样它就可以被称为类似非模板函数吗?

例如,

// some template function
template <typename T>
void foo(T t) {...}
// I would like int_foo to behave like a regular function with
// foo<int> as its implementation (obviously this doesn't compile because
// using cannot be used like this)
using int_foo = foo<int>;

一种方法是只委派并依靠内联来完成其余的工作,例如:

void int_foo(int i) {
    foo<int>(i);
}

。但如果有更直接的方法,我全听。

不,没有,不需要 30 个字符,但所有答案都需要。