std::function 是一个类型擦除模板。类型推导与类型擦除相反(几乎相反)。
类型推断类型擦除模板是代码异味。而且它很少起作用。
c++17里有扣分指南,可以这样操作:
template<typename... t_args>
void add_command(const std::string& name, const std::function<void(t_args...)>& args)
void add_command(const std::string& name, t_func const& func) {
std::function f = func;
add_command(name, f);
}
这是不完美的,但不可能有完美的解决方案。
推理指导look like:
template<class R, class... ArgTypes>
function(R(*)(ArgTypes...)) -> function<R(ArgTypes...)>;
template<class F>
function(F) -> function</*see below*/>;
它通过检查&F::operator() 提取function 的签名。这可能会因重载或模板而失败。当然,这不适用于重载的函数名称。
您可以在 c++11 中使用函数特征类复制此内容:
template<class X>
struct function_traits:function_traits<decltype(&X::operator())> {};
#define MEM_FUN_HELPER2(...) \
template<class R, class T, class...Args> \
struct function_traits<R(T::*)(Args...) __VA_ARGS__>:function_traits<R(Args...)>{}; \
template<class R, class T, class...Args> \
struct function_traits<R(T::*)(Args..., ...) __VA_ARGS__>:function_traits<R(Args..., ...)>{}; \
template<class R, class T, class...Args> \
struct function_traits<R(T::*)(Args...) __VA_ARGS__ noexcept>:function_traits<R(Args...) noexcept>{}; \
template<class R, class T, class...Args> \
struct function_traits<R(T::*)(Args..., ...) __VA_ARGS__ noexcept>:function_traits<R(Args..., ...) noexcept>{}
#define MEM_FUN_HELPER1(...) \
MEM_FUN_HELPER2(__VA_ARGS__); \
MEM_FUN_HELPER2(__VA_ARGS__ &); \
MEM_FUN_HELPER2(__VA_ARGS__ &&)
#define MEM_FUN_HELPER0(...) \
MEM_FUN_HELPER1(__VA_ARGS__); \
MEM_FUN_HELPER1(const __VA_ARGS__)
#define MEM_FUN_HELPER() \
MEM_FUN_HELPER0(); \
MEM_FUN_HELPER0(volatile)
MEM_FUN_HELPER();
template<class R, class...Args>
struct function_traits<R(*)(Args...)>:function_traits<R(Args...)>{};
template<class R, class...Args>
struct function_traits<R(*)(Args..., ...)>:function_traits<R(Args..., ...)>{};
template<class R, class...Args>
struct function_traits<R(*)(Args...) noexcept>:function_traits<R(Args...) noexcept>{};
template<class R, class...Args>
struct function_traits<R(*)(Args..., ...) noexcept>:function_traits<R(Args..., ...) noexcept>{};
template<class R, class...Args>
struct function_traits<R(Args...) noexcept> : function_traits<R(Args...)> {
enum {is_noexcept=true};
};
template<class R, class...Args>
struct function_traits<R(Args..., ...) noexcept> : function_traits<R(Args..., ...)> {
enum {is_noexcept=true};
};
template<class R, class...Args>
struct function_traits<R(Args...)> {
template<template<class...>class Z>
using transcribe=Z<R(Args...)>;
using std_function = transcribe<std::function>;
using result_type = R;
using arg_tuple = std::tuple<Args...>;
enum{is_noexcept=false};
};
template<class R, class...Args>
struct function_traits<R(Args..., ...)> {
template<template<class...>class Z>
using transcribe=Z<R(Args..., ...)>;
using std_function = transcribe<std::function>;
using result_type = R;
// doesn't really work, but what ya gonna do:
using arg_tuple = std::tuple<Args...>;
enum{is_noexcept=false};
};
这太疯狂了; MEM_FUN_HELPER(); 扩展为 48 个模板特化。 3 个 ref 限定符(&、&& 什么都没有),然后是 4 个其他必须“手动”处理的东西(const、volatile、noexcept 和 ... C 风格的可变参数)。
无论如何,一旦你有了它,你就可以这样做:
template<typename... t_args>
void add_command(const std::string& name, const std::function<void(t_args...)>& args)
template<class t_func>
void add_command(const std::string& name, t_func const& func) {
typename function_traits<t_func>::std_function f = func;
add_command(name, f);
}
在c++11。这(大致和不完美)与c++17 演绎指南相同。
Live example.