https://github.com/mnikander/cpp_bind
Prototype for a concise argument binding syntax to replace lambda expressions in some contexts
https://github.com/mnikander/cpp_bind
Last synced: 3 months ago
JSON representation
Prototype for a concise argument binding syntax to replace lambda expressions in some contexts
- Host: GitHub
- URL: https://github.com/mnikander/cpp_bind
- Owner: mnikander
- License: mit
- Created: 2024-06-13T14:17:39.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-01-24T09:56:48.000Z (5 months ago)
- Last Synced: 2025-01-24T10:28:52.733Z (5 months ago)
- Language: C++
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Argument binding
Prototype a concise syntax for argument binding, using operator overloading.
## Getting started
1. Install dependencies
```bash
sudo apt get install libgtest-dev cmake g++
```2. Clone the repo
```bash
git clone #...
```3. Build and run:
```bash
# out-of-source build
cd cpp_bind && mkdir out && cd out && cmake .. && cd ..
cmake --build out/ && ./out/unit_tests
```## Simple example
Create function objects where one argument of a binary function has been bound:
```cpp
auto twoMinus = 2 >>= std::minus<>{}; // bind left argument to 2
auto minusTwo = std::minus<>{} <<= 2; // bind right argument to 2
```## Usage example: subtract one from each element
Given:
```cpp
const std::vector v{1, 2, 4, 8};
std::vector result(v.size());
```This is how we would normally subtract one from each element in a sequence:
```cpp
std::transform(v.cbegin(),
v.cend(),
0,
[](int i){ return i-1; });
```With the argument binding enabled, we can instead write it like this:
```cpp
using namespace bind; // make the overloads for operator>>= and operator<<= availablestd::transform(v.cbegin(),
v.cend(),
0,
std::minus<>{} <<= 1); // bind right argument of 'minus' to equal 1
```For binary functions which have their own operator symbol, such as + - * / ect, a lambda expression is shorter, but for
function-like objects without its own special symbol, the overload syntax is shorter and simpler.