https://github.com/curve/lambda
🔧 A C++ utility for working with C-Libraries
https://github.com/curve/lambda
Last synced: 9 months ago
JSON representation
🔧 A C++ utility for working with C-Libraries
- Host: GitHub
- URL: https://github.com/curve/lambda
- Owner: Curve
- Created: 2023-05-30T20:02:59.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-06-12T18:57:21.000Z (about 3 years ago)
- Last Synced: 2025-10-09T03:03:20.048Z (9 months ago)
- Language: C++
- Size: 43 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### lambda
A utility for working with C-Libraries from C++
---
### Basic Examples
> Using lambdas with capture as a function pointer
```cpp
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
```cpp
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](tests/)!
---
### Installation
> **Note**
> This library requires a C++20 capable compiler.
- FetchContent
```cmake
include(FetchContent)
FetchContent_Declare(lambda GIT_REPOSITORY "https://github.com/Curve/lambda")
FetchContent_MakeAvailable(lambda)
target_link_libraries( lambda::ptr)
```
- Git Submodule
```bash
git submodule add "https://github.com/Curve/lambda"
```
```cmake
# Somewhere in your CMakeLists.txt
add_subdirectory("")
target_link_libraries( lambda::ptr)
```
---
### Windows
**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
```cpp
some_c_function(lambda::pointer_to([&]{/*...*/}));
```
```cpp
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