An open API service indexing awesome lists of open source software.

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

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<<= available

std::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.