目前,这在一般意义上是不可能做到的,但如果您的 lambda 的所有参数都具有 auto 功能,并且可以将所有参数替换为某种已知类型,那么您可以修改此问题的答案:C++ 元函数来确定类型是否可调用 https://stackoverflow.com/questions/5100015/c-metafunction-to-determine-whether-a-type-is-callable
沿着
static const bool OneArg = (sizeof( test<T, int>(0) ) == 1);
static const bool TwoArg = (sizeof( test<T, int, int>(0) ) == 1);
static const bool ThreeArg = (sizeof( test<T, int, int, int>(0) ) == 1);
static constexpr std::size_t TemplatedOperatorArgSize =
OneArg
? 1
: TwoArg
? 2
: ThreeArg
? 3
: -1;
对嵌套三元组表示歉意。然后使用类似这样的东西来调用它:
template<size_t N, typename T>
struct Apply {
template<typename F, typename... A>
static inline decltype(auto) apply(F && f, A &&... a) {
return Apply<N-1, T>::apply(::std::forward<F>(f), std::declval<T>(), ::std::forward<A>(a)...);
}
};
template<typename T>
struct Apply<0, T> {
template<typename F, typename... A>
static inline decltype(auto) apply(F && f, A &&... a) {
return invoke(std::forward<F>(f), std::forward<A>(a)...);
}
};
template<std::size_t Size, typename T, typename F>
inline decltype(auto) apply(F && f) {
return Apply<Size, T>::apply(::std::forward<F>(f));
}
使用调用来自:http://en.cppreference.com/w/cpp/utility/function/invoke http://en.cppreference.com/w/cpp/utility/functional/invoke
获取返回类型
using return_type = decltype(
apply<has_callable_operator<T>::TemplatedOperatorArgSize, int>(function)
);
第一部分可以重写以处理任意数量的参数,使用std::make_index_sequence
但总的来说,我会说你正在掉进兔子洞。祝你好运。
注意:没有测试应用代码,但它应该足以让您继续使用。
编辑:此外,返回类型需要独立于参数类型才能实现您想要的