是否可以将lambda函数作为函数指针传递?如果是这样,我一定是做了错误的事情,因为我得到了一个编译错误。

考虑下面的例子

using DecisionFn = bool(*)();

class Decide
{
public:
    Decide(DecisionFn dec) : _dec{dec} {}
private:
    DecisionFn _dec;
};

int main()
{
    int x = 5;
    Decide greaterThanThree{ [x](){ return x > 3; } };
    return 0;
}

当我尝试编译这个时,我得到以下编译错误:

In function 'int main()':
17:31: error: the value of 'x' is not usable in a constant expression
16:9:  note: 'int x' is not const
17:53: error: no matching function for call to 'Decide::Decide(<brace-enclosed initializer list>)'
17:53: note: candidates are:
9:5:   note: Decide::Decide(DecisionFn)
9:5:   note: no known conversion for argument 1 from 'main()::<lambda()>' to 'DecisionFn {aka bool (*)()}'
6:7:   note: constexpr Decide::Decide(const Decide&)
6:7:   note: no known conversion for argument 1 from 'main()::<lambda()>' to 'const Decide&'
6:7:   note: constexpr Decide::Decide(Decide&&)
6:7:   note: no known conversion for argument 1 from 'main()::<lambda()>' to 'Decide&&'

这是一个要消化的错误消息,但我认为我从中得到的是不能作为constexpr处理,因此我不能将它作为函数指针传递?我也试着让x成为constexpr,但这似乎没有帮助。


当前回答

使用lambda with作为C函数指针的快捷方式是:

"auto fun = +[](){}"

以Curl为例(Curl调试信息)

auto callback = +[](CURL* handle, curl_infotype type, char* data, size_t size, void*){ //add code here :-) };
curl_easy_setopt(curlHande, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curlHande,CURLOPT_DEBUGFUNCTION,callback);

其他回答

正如其他人提到的,你可以用Lambda函数代替函数指针。我使用这个方法在我的c++接口到F77 ODE求解器RKSUITE。

//C interface to Fortran subroutine UT
extern "C"  void UT(void(*)(double*,double*,double*),double*,double*,double*,
double*,double*,double*,int*);

// C++ wrapper which calls extern "C" void UT routine
static  void   rk_ut(void(*)(double*,double*,double*),double*,double*,double*,
double*,double*,double*,int*);

//  Call of rk_ut with lambda passed instead of function pointer to derivative
//  routine
mathlib::RungeKuttaSolver::rk_ut([](double* T,double* Y,double* YP)->void{YP[0]=Y[1]; YP[1]= -Y[0];}, TWANT,T,Y,YP,YMAX,WORK,UFLAG);

Shafik Yaghmour的答案正确地解释了为什么lambda不能作为函数指针传递,如果它有一个捕获。我想介绍两个简单的解决方法。

Use std::function instead of raw function pointers. This is a very clean solution. Note however that it includes some additional overhead for the type erasure (probably a virtual function call). #include <functional> #include <utility> struct Decide { using DecisionFn = std::function<bool()>; Decide(DecisionFn dec) : dec_ {std::move(dec)} {} DecisionFn dec_; }; int main() { int x = 5; Decide greaterThanThree { [x](){ return x > 3; } }; } Use a lambda expression that doesn't capture anything. Since your predicate is really just a boolean constant, the following would quickly work around the current issue. See this answer for a good explanation why and how this is working. // Your 'Decide' class as in your post. int main() { int x = 5; Decide greaterThanThree { (x > 3) ? [](){ return true; } : [](){ return false; } }; }

虽然模板方法出于各种原因很聪明,但重要的是要记住lambda和捕获的变量的生命周期。如果要使用任何形式的lambda指针is,并且lambda不是向下延续,那么只应该使用复制[=]lambda。也就是说,即使这样,如果捕获的指针的生命周期(堆栈unwind)比lambda的生命周期短,那么在堆栈上捕获一个指向变量的指针也是不安全的。

捕获lambda作为指针的一个更简单的解决方案是:

auto pLamdba = new std::function<...fn-sig...>([=](...fn-sig...){...});

例如,新std::函数<空白 ()>([=]() -> 空白{…}

只需要记住稍后删除pLamdba,以确保不会泄漏lambda内存。 这里要意识到的秘密是lambdas可以捕获lambdas(问问自己这是如何工作的),而且为了让std::function正常工作,lambda实现需要包含足够的内部信息来提供对lambda(和捕获的)数据大小的访问(这就是为什么delete应该工作[运行捕获类型的析构函数])。

这是另一种解决方案。c++ 14(可以转换为c++ 11)支持返回值、不可复制和可变lambda。如果不需要可变的lambdas,可以通过删除匹配非const版本的专门化并嵌入impl_impl来更短。

对于那些想知道的人来说,它是有效的,因为每个lambda都是唯一的(是不同的类),因此调用to_f会为这个lambda静态和相应的c风格函数生成唯一的,可以访问它。

template <class L, class R, class... Args> static auto impl_impl(L l) {
  static_assert(!std::is_same<L, std::function<R(Args...)>>::value,
                "Only lambdas are supported, it is unsafe to use "
                "std::function or other non-lambda callables");

    static L lambda_s = std::move(l);
    return +[](Args... args) -> R { return lambda_s(args...); };
}

template <class L>
struct to_f_impl : public to_f_impl<decltype(&L::operator())> {};
template <class ClassType, class R, class... Args>
struct to_f_impl<R (ClassType::*)(Args...) const> {
  template <class L> static auto impl(L l) {
    return impl_impl<L, R, Args...>(std::move(l));
  }
};
template <class ClassType, class R, class... Args>
struct to_f_impl<R (ClassType::*)(Args...)> {
  template <class L> static auto impl(L l) {
    return impl_impl<L, R, Args...>(std::move(l));
  }
};

template <class L> auto to_f(L l) { return to_f_impl<L>::impl(std::move(l)); }

注意,这也适用于其他可调用的对象,如std::function,但如果它不起作用会更好,因为与lambdas不同,std::function这样的对象不会生成唯一类型,因此内部模板和它的内部静态将被所有具有相同签名的函数重用/共享,这很可能不是我们想从它那里得到的。我已经明确禁止std::函数,但还有更多我不知道如何以通用的方式禁止。

一个类似的答案,但我这样做是为了让你不必指定返回指针的类型(注意,通用版本需要c++ 20):

#include <iostream>


template<typename Function>
struct function_traits;

template <typename Ret, typename... Args>
struct function_traits<Ret(Args...)> {
    typedef Ret(*ptr)(Args...);
};

template <typename Ret, typename... Args>
struct function_traits<Ret(*const)(Args...)> : function_traits<Ret(Args...)> {};

template <typename Cls, typename Ret, typename... Args>
struct function_traits<Ret(Cls::*)(Args...) const> : function_traits<Ret(Args...)> {};

using voidfun = void(*)();

template <typename F>
voidfun lambda_to_void_function(F lambda) {
    static auto lambda_copy = lambda;

    return []() {
        lambda_copy();
    };
}

// requires C++20
template <typename F>
auto lambda_to_pointer(F lambda) -> typename function_traits<decltype(&F::operator())>::ptr {
    static auto lambda_copy = lambda;
    
    return []<typename... Args>(Args... args) {
        return lambda_copy(args...);
    };
}



int main() {
    int num;

    void(*foo)() = lambda_to_void_function([&num]() {
        num = 1234;
    });
    foo();
    std::cout << num << std::endl; // 1234

    int(*bar)(int) = lambda_to_pointer([&](int a) -> int {
        num = a;
        return a;
    });
    std::cout << bar(4321) << std::endl; // 4321
    std::cout << num << std::endl; // 4321
}