是否可以将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没有捕获,则只能将其转换为函数指针,摘自c++ 11标准草案5.1.2节[expr.prim. xml]。Lambda]表示(强调我的):
没有lambda捕获的lambda表达式的闭包类型具有
到指针的公共非虚拟非显式const转换函数
函数具有与闭包相同的参数和返回类型
类型的函数调用操作符。此转换返回的值
Function应是函数的地址,该函数在调用时具有
与调用闭包类型的函数调用操作符的效果相同。
注意,cppreference在Lambda函数一节中也介绍了这一点。
因此,以下选择是可行的:
typedef bool(*DecisionFn)(int);
Decide greaterThanThree{ []( int x ){ return x > 3; } };
这个也一样:
typedef bool(*DecisionFn)();
Decide greaterThanThree{ [](){ return true ; } };
正如5gon12eder所指出的,您也可以使用std::function,但请注意std::function是很重要的,所以它不是一个无成本的权衡。
一个类似的答案,但我这样做是为了让你不必指定返回指针的类型(注意,通用版本需要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
}
如果lambda没有捕获,则只能将其转换为函数指针,摘自c++ 11标准草案5.1.2节[expr.prim. xml]。Lambda]表示(强调我的):
没有lambda捕获的lambda表达式的闭包类型具有
到指针的公共非虚拟非显式const转换函数
函数具有与闭包相同的参数和返回类型
类型的函数调用操作符。此转换返回的值
Function应是函数的地址,该函数在调用时具有
与调用闭包类型的函数调用操作符的效果相同。
注意,cppreference在Lambda函数一节中也介绍了这一点。
因此,以下选择是可行的:
typedef bool(*DecisionFn)(int);
Decide greaterThanThree{ []( int x ){ return x > 3; } };
这个也一样:
typedef bool(*DecisionFn)();
Decide greaterThanThree{ [](){ return true ; } };
正如5gon12eder所指出的,您也可以使用std::function,但请注意std::function是很重要的,所以它不是一个无成本的权衡。
使用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);
捕获lambdas不能转换为函数指针,正如这个答案所指出的。
然而,为只接受一个函数指针的API提供函数指针通常是相当痛苦的。最常被引用的方法是提供一个函数,并用它调用一个静态对象。
static Callable callable;
static bool wrapper()
{
return callable();
}
这太乏味了。我们进一步利用了这个想法,自动化了创建包装器的过程,使工作变得更加简单。
#include<type_traits>
#include<utility>
template<typename Callable>
union storage
{
storage() {}
std::decay_t<Callable> callable;
};
template<int, typename Callable, typename Ret, typename... Args>
auto fnptr_(Callable&& c, Ret (*)(Args...))
{
static bool used = false;
static storage<Callable> s;
using type = decltype(s.callable);
if(used)
s.callable.~type();
new (&s.callable) type(std::forward<Callable>(c));
used = true;
return [](Args... args) -> Ret {
return Ret(s.callable(std::forward<Args>(args)...));
};
}
template<typename Fn, int N = 0, typename Callable>
Fn* fnptr(Callable&& c)
{
return fnptr_<N>(std::forward<Callable>(c), (Fn*)nullptr);
}
并将其用作
void foo(void (*fn)())
{
fn();
}
int main()
{
int i = 42;
auto fn = fnptr<void()>([i]{std::cout << i;});
foo(fn); // compiles!
}
Live
这实际上是在每次出现fnptr时声明一个匿名函数。
请注意,给定相同类型的可调用对象,fnptr的调用将覆盖先前编写的可调用对象。在某种程度上,我们用int参数N来补救这个问题。
std::function<void()> func1, func2;
auto fn1 = fnptr<void(), 1>(func1);
auto fn2 = fnptr<void(), 2>(func2); // different function
这不是一个直接的答案,而是使用“functor”模板模式来隐藏lambda类型的细节,并保持代码的美观和简单。
我不确定你想如何使用决定类,所以我必须用一个使用它的函数扩展类。完整示例请参见:https://godbolt.org/z/jtByqE
类的基本形式可能是这样的:
template <typename Functor>
class Decide
{
public:
Decide(Functor dec) : _dec{dec} {}
private:
Functor _dec;
};
将函数类型作为类类型的一部分传入,如下所示:
auto decide_fc = [](int x){ return x > 3; };
Decide<decltype(decide_fc)> greaterThanThree{decide_fc};
再一次,我不确定为什么你要捕捉x它更有意义(对我来说)有一个参数,你传递给lambda),所以你可以像这样使用:
int result = _dec(5); // or whatever value
有关完整示例,请参见链接