Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从lambda推导std::函数参数_C++_C++11_Lambda - Fatal编程技术网

C++ 从lambda推导std::函数参数

C++ 从lambda推导std::函数参数,c++,c++11,lambda,C++,C++11,Lambda,我试图在std::function中接受多个参数,但我得到了: #包括 样板 无效调用方(std::function) { } int main() { 调用方([&](){}); } 错误是: main.cpp:11:22: error: no matching function for call to 'caller(main()::<lambda()>)' caller([&] () { }); ^ main.cp

我试图在
std::function
中接受多个参数,但我得到了:

#包括
样板
无效调用方(std::function)
{
}
int main()
{
调用方([&](){});
}
错误是:

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([&] () { });
main.cpp:11:22:错误:调用“caller(main():)”时没有匹配的函数
调用方([&](){});
^
main.cpp:11:22:注:候选人为:
main.cpp:4:6:注意:模板void调用者(std::function)
无效调用方(std::function)
^
main.cpp:4:6:注意:模板参数扣除/替换失败:
main.cpp:11:22:注意:“main()::”不是从“std::function”派生的
调用方([&](){});
我怎样才能做到这一点呢?

也许吧

#include <functional>
#include <iostream>

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

}

void f() {
    caller<void()>([&] () {
        std::cout << "Hi." << std::endl;
    });
}
#包括
#包括
样板
无效调用方(std::function)
{
}
void f(){
调用方([&](){

std::cout Hello!请提供一个完整的示例,并指定您得到的确切错误。@BjörnPollex-Edited.related:
#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;
    });
}