template<typename Type>
void execute(absl::string_view expected_name){
std::cout << "*** expected type: " << expected_name
<< " | actual type: " << typeid(Type);
}
void handle(std::function<void(absl::string_view)> executor){
executor<int>("int");
executor<double>("double");
}
我有这段代码(当然,它没有编译)。 我希望能够将模板化函数传递给“普通”函数,并让“普通”函数根据需要定义具体类型,如示例所示。
有没有办法在handle
参数列表上声明executor
是一个模板函数?
最佳答案
但是 handler()
必须接收一个 std::function
吗?
如果您可以传递一个带有模板函数的struct
,比如说
struct executor
{
template <typename T>
void func (std::string_view const & sw) const
{ execute<T>(sw); }
};
你可以编写handler()
如下
void handle (executor const & ex)
{ ex.func<int>("int"); ex.func<double>("double"); }
关于c++ - 将模板函数声明为非模板函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48993606/