2013-11-21 78 views
0

你好,我是试图建立一个功能,这将使调用函数提供的参数与一些提供参数和返回它的价值,我目前的做法如下推断拉姆达返回类型

#include <functional> 
    #include <iostream> 
    #include <type_traits> 


    template <typename Res,typename... T> 
    auto func(typename std::common_type<std::function<Res(T...)>>::type f, T... values) -> decltype(f(values...)) { 
    f(values...); 
    } 

    int fx(int x,int y){return x+y; } 
    int main() { 
>> func([](int x, int y, int z) { std::cout << (x*y*z) << std::endl; }, 3, 6, 8); 
    return 0; 
    } 

如果我用void替换Res并删除它的自动工作,但不知何故我无法获得正确的返回类型。

回答

2

最简单的方法就是接受任何仿函数,而不仅仅是std::function

template <typename F,typename... T> 
auto func(F f, T... values) -> decltype(f(values...)) { 
    return f(values...); 
}