Using lambdas with capture as a function pointer
extern "C" void some_c_function(void (*callback)(int a))
{
// ...
}
std::string some_string;
some_c_function(lambda::pointer_to([&](int)
{
some_string = "Captured by reference!";
}));
A utility for passing
void*
user data
extern "C" void some_c_function(void (*callback)(int a, void* user_data), void* user_data)
{
// ...
}
std::string some_string;
auto user_data = lambda::user_data(some_string);
some_c_function([](int a, void* data) {
auto &[some_string] = decltype(user_data)::from(data).tuple();
some_string = "Easy user-data!";
}, user_data);
For more examples, and some useful comments, you might want to take a look at the tests!
Note
This library requires a C++20 capable compiler.
-
FetchContent
include(FetchContent) FetchContent_Declare(lambda GIT_REPOSITORY "https://github.com/Curve/lambda") FetchContent_MakeAvailable(lambda) target_link_libraries(<YourLibrary> lambda::ptr)
-
Git Submodule
git submodule add "https://github.com/Curve/lambda"
# Somewhere in your CMakeLists.txt add_subdirectory("<path_to_lambda>") target_link_libraries(<YourLibrary> lambda::ptr)
Q: Does this Library work on windows?
A: Yes
Q: Does this library work with MSVC?
A: Kind of, beware that lambda captures on MSVC are broken¹ and MSVC Versions below 19.36 are affected by a bug that causes compilation errors.
¹: This bug has already been reported and will hopefully be fixed with a new MSVC update.
❌ Broken | ✅ Working |
---|---|
some_c_function(lambda::pointer_to([&]{/*...*/})); |
auto fn_ptr = lambda::pointer_to([&]{/*...*/});
some_c_function(fn_ptr); |
Q: Are there alternatives to MSVC?
A: Yes! I'd recommend to just use clang-cl