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;
});
}