c++可变模板std::函数到lambda的转换不工作
C++ variadic template std::function to lambda conversion not working
我有以下代码(简化):
#include <functional>
template <typename... Args> void Callback(std::function<void(Args...)> f){
// store f and call later
}
int main(){
Callback<int, float>([](int a, float b){
// do something
});
}
这样做的目的是获取一些额外的参数,发送一个数据包,处理响应并使用结果调用lambda问题是,它不接受lambda
# g++ -std=c++11 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:8:3: error: no matching function for call to ‘Callback(main()::<lambda(int, float)>)’
test.cpp:8:3: note: candidate is:
test.cpp:2:34: note: template<class ... Args> void Callback(std::function<void(Args ...)>)
test.cpp:2:34: note: template argument deduction/substitution failed:
test.cpp:8:3: note: ‘main()::<lambda(int, float)>’ is not derived from ‘std::function<void(Args ...)>’
是否有任何方法可以让它工作,而不需要通过在std::函数中显式包装lambda的麻烦?
Callback(std::function<void(int, float)>([](int a, float b){
// do something
}));
可以完美地工作,即使省略了Callback模板参数(如下所示)。还有一个额外的std::函数
为什么它不能自己计算出转换?它适用于非模板:
void B(std::function<void(int, float)> f){/* ... */};
int main(){
B([](int a, float b){
// do something
});
}
作为参考,我使用
gcc version 4.7.2 (Debian 4.7.2-5)
您可以使用专用类型转换。一旦你有了这样的工具
#include <functional>
using namespace std;
template<typename T>
struct memfun_type
{
using type = void;
};
template<typename Ret, typename Class, typename... Args>
struct memfun_type<Ret(Class::*)(Args...) const>
{
using type = std::function<Ret(Args...)>;
};
template<typename F>
typename memfun_type<decltype(&F::operator())>::type
FFL(F const &func)
{ // Function from lambda !
return func;
}
你可以将FFL()
转换为所有lambda类型,将它们转换为std::function
的正确版本
int main()
{
Callback(FFL([](int a, float b){
// do something
}));
return 0;
}
显示正如Praetorian指出的:这回答了我的问题。我真的很困惑为什么你需要使用吮吸黑客。参考代码:
#include <functional>
template <typename T> struct identity
{
typedef T type;
};
template <typename... Args> void Callback(typename identity<std::function<void(Args...)>>::type f){
// store f and call later
}
int main(){
Callback<int, float>([](int a, float b){
// do something
});
}
当lambda不捕获时,您可以使用:
template <typename L> void Callback(L l){
using F = typename std::remove_pointer<decltype(+l)>::type;
std::function<F> f( l );
// store f and call later
}
允许你调用:
Callback([](int a, float b){
// do something
});
<<p> 生活例子/strong> 相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- C++:Lambda 函数指针转换的用例是什么?
- 错误:无法将"<lambda(double)>"转换为"double(*)(double)"
- 如何将 boost::hana::map 转换为 lambda
- 将 lambda 函数转换为具有混合 lambda 引入器和参数列表的函子结构
- 如何将函数转换为 lambda 函数
- 将转发 lambda 转换为函数指针
- 如何将C++ lambda 函数代码转换为 C#?
- 使用动态强制转换的 Lambda
- lambda 转换为布尔值,而不是推导函数指针类型
- Lambda 转换的函数指针比较
- 将 lambda 转换为函数指针类型定义
- 为什么 C++11 不隐式将 lambda 转换为 std::function 对象?
- C++11:将lambda转换为源代码中的函数对象
- 为什么我的C++程序在将 lambda 转换为函数指针时崩溃
- 将 lambda 转换为 std::函数时出现问题
- 存储在std::函数中时,无法将无捕获lambda转换为函数指针
- 与GCC/MSVC中的lambda转换构造函数不一致
- 隐式地将lambda转换为boost::函数
- 将存储在std::function中的带有捕获子句的lambda转换为原始函数指针