小编coc*_*oco的帖子

模板函数的模板函数参数“忽略候选模板:无法匹配...”

概述

我正在尝试将 lambda 传递给采用模板函数类型的模板函数。编译时出现错误candidate template ignored: could not match...

但是,当我尝试将相同的 lambda 传递给采用模板函数类型的模板类时,它会编译并运行。

问题

  1. 为什么这适用于模板类而不适用于模板函数?
  2. 有办法让这个工作吗?
  3. 当我使用模板函数尝试此操作时,模板函数类型的额外预期参数是什么(有关更多详细信息,请参阅下文)?

代码

考虑以下代码 (c++17)

#include <functional>

// define the template function type
template<typename T, typename...S>
using compose_fn_t = std::function<T(S...)>;


// define a template function which accepts the template function type
template<typename T, typename... S>
void compose(compose_fn_t<T, S...> fn) {};

// define a template class which accepts the template function type
template<typename T, typename... S>
class Compose {
public:
  Compose(compose_fn_t<T, S...> fn) {};
}; …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++17

2
推荐指数
1
解决办法
5830
查看次数

标签 统计

c++ ×1

c++17 ×1

templates ×1

variadic-templates ×1