从lambda中减去std :: function参数

时间:2014-09-19 14:50:27

标签: c++ c++11 lambda

我正在尝试接受std::function中的多个参数,但我得到errors

#include <functional>

template <typename... Args>
void caller(std::function<void(Args&&...)> function)
{

}

int main()
{
    caller([&] () { });
}

错误是:

main.cpp:11:22: error: no matching function for call to 'caller(main()::<lambda()>)'
     caller([&] () { });
                      ^
main.cpp:11:22: note: candidate is:
main.cpp:4:6: note: template<class ... Args> void caller(std::function<void(Args&& ...)>)
 void caller(std::function<void(Args&&...)> function)
      ^
main.cpp:4:6: note:   template argument deduction/substitution failed:
main.cpp:11:22: note:   'main()::<lambda()>' is not derived from 'std::function<void(Args&& ...)>'
     caller([&] () { });

我该如何做到这一点?

1 个答案:

答案 0 :(得分:-2)

也许

#include <functional>
#include <iostream>

template<typename F>
void caller(std::function<F> function)
{

}

void f() {
    caller<void()>([&] () {
        std::cout << "Hi." << std::endl;
    });
}

甚至

#include <functional>
#include <iostream>

template<typename F>
void caller(F function)
{

}

void f() {
    caller<void()>([&] () {
        std::cout << "Hi." << std::endl;
    });
}

两个代码都编译,但我不知道如果我不知道你要做什么它们对你有什么用呢......

相关问题