C++ C++;将函数参数类型用于模板参数推断
C++ C++;将函数参数类型用于模板参数推断,c++,templates,c++11,c++14,functor,C++,Templates,C++11,C++14,Functor,f中的fall tofunc应该可以由编译器内联,因此不能使用std::function。我通常使用类似于(GPL-3许可)的函数特性代码: template<typename T, typename Function> void f(Function func); template<typename Function> void call_f(Function func) { using arg_type = first_argument_type_t<
f
中的fall tofunc
应该可以由编译器内联,因此不能使用std::function
。我通常使用类似于(GPL-3许可)的函数特性代码:
template<typename T, typename Function> void f(Function func);
template<typename Function>
void call_f(Function func) {
using arg_type = first_argument_type_t<Function>; // ???
f<arg_type, Function>(func);
}
模板
使用第一个参数类型=
typename sharemind::FunctionTraits::模板参数::类型;
但是还有另一种选择,这可能是有帮助的。
有很多要考虑的参数来推断参数类型。在一般情况下,如果那个函子是通用lambda,或者类类型实例有重载/模板<代码>运算符()/<代码>,则不能实现。template<typename T, typename Function> void f(Function func);
template<typename Function>
void call_f(Function func) {
using arg_type = first_argument_type_t<Function>; // ???
f<arg_type, Function>(func);
}
template <typename F>
using first_argument_type_t =
typename sharemind::FunctionTraits<F>::template argument<0u>::type;