这似乎适用于我尝试过的平台:
#include <iostream>
// extern "C" linkage
extern "C" void foo(void (*fn_ptr)(int));
namespace {
struct bar {
static void f(int);
};
}
int main() {
// Usually works on most platforms, not guaranteed though:
foo(bar::f);
// Probably equally bad or worse?
foo([](int x) { std::cout << x << std::endl; });
}
但又是passing a static member function also worked on these platforms when it was not required to。
有没有办法强制lambda具有合适的连接以使其安全和便携?或者它已经是?
答案 0 :(得分:6)
没有。 Lambda最终是具有函数调用运算符的对象。无捕获的lambda可以转换为适当类型的函数指针,但该函数指针将是带有C ++链接的C ++函数。