c++可变模板std::函数到lambda的转换不工作

C++ variadic template std::function to lambda conversion not working

本文关键字:lambda 转换 工作 函数 std c++      更新时间:2023-10-16

我有以下代码(简化):

#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>