https://github.com/cihansari/flexinvokable
Single header container for lambdas with (or without) move-only encapsulation that can be invoked only once.
https://github.com/cihansari/flexinvokable
cpp cpp17 cpp1z
Last synced: 2 months ago
JSON representation
Single header container for lambdas with (or without) move-only encapsulation that can be invoked only once.
- Host: GitHub
- URL: https://github.com/cihansari/flexinvokable
- Owner: CihanSari
- License: mit
- Created: 2018-04-13T08:22:10.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2021-11-10T08:52:12.000Z (over 3 years ago)
- Last Synced: 2025-03-13T23:13:41.462Z (2 months ago)
- Topics: cpp, cpp17, cpp1z
- Language: C++
- Homepage:
- Size: 133 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Flexible invokable for C++17
Single header container for lambdas with (or without) move-only encapsulation that can be invoked only once.
The topic of this repository is to address the need to store lambdas in containers (such as vectors). The generic way you are expected to do this is in [std::function](https://en.cppreference.com/w/cpp/utility/functional/function). However, `std::function` requires the encapsulated contents to be copy constructable. This is easy to imagine from the above struct: encapsulations sometimes cause issue! If you have members that are not copiable, then you cannot use `std::function` even though contents are movable. This is the need we are trying to address in `flexInvokable`. You can store your lambdas in move-only containers.
[](https://github.com/CihanSari/flexInvokable/actions/workflows/main.yml)
[](https://github.com/CihanSari/flexInvokable/actions/workflows/codeql-analysis.yml)## Example
Example is shown in `test/src/example.cpp`.## How to use
Add flexInvokable/include/flexInvokable.hpp to your project.## Source
Originally found as an answer to the request in [stackoverflow](https://stackoverflow.com/questions/25421346/how-to-create-an-stdfunction-from-a-move-capturing-lambda-expression).I had to make slight changes from [source demo](http://coliru.stacked-crooked.com/a/1cfdcd0491686fe1)
```
void(*invoke)(void*, Args...) = nullptr;
```
to
```
R(*invoke)(void*, Args...) = nullptr;
```
Moreover,
```
std::result_of_t&(Args...)>
```
is getting deprecated, and therefore replaced to
```
#if _HAS_CXX17
std::invoke_result_t
#else
std::result_of_t&(Args...)>
#endif
```