https://github.com/tradias/lambda-tuple
An implementation of `std::tuple` based on variadic lambda capture
https://github.com/tradias/lambda-tuple
compile-time cpp cpp20 header-only lambda tuple
Last synced: 4 months ago
JSON representation
An implementation of `std::tuple` based on variadic lambda capture
- Host: GitHub
- URL: https://github.com/tradias/lambda-tuple
- Owner: Tradias
- License: mit
- Created: 2022-08-03T14:11:13.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-09-16T18:13:22.000Z (over 1 year ago)
- Last Synced: 2025-06-14T09:03:56.438Z (8 months ago)
- Topics: compile-time, cpp, cpp20, header-only, lambda, tuple
- Language: C++
- Homepage:
- Size: 71.3 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lambda-tuple
An implementation of `std::tuple` based on variadic lambda capture ([P0780R2](https://open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0780r2.html)) added in C++20.
The idea is to store all elements of the tuple in such a lambda:
```cpp
[... v = std::move(v)](auto f) mutable -> decltype(auto)
{
return f(v...);
}
```
I would consider it more of a toy implementation. Nonetheless, there is an extensive test suite including several tests from the Microsoft STL amounting to 75+ tests in total.
Play with it on [godbolt](https://godbolt.org/z/ajxavsrnM).
## Advantages
* Triviallity of copy/move construction of types is preserved.
* No template or function recursion used in the implementation.
* Automatic pretty-printing in the debugger. E.g. for `tuple`

* An empty tuple is `std::is_trivial_v`.
* Roughly 2.6 times faster to compile with GCC and 2 times faster with Clang. ([build-bench](https://build-bench.com/b/OOEJlYo9mQObmYHxBtS8G6-cmAc))
## Disadvantages
* Cannot be passed across DLL boundaries.
* No in-place construction, just like `std::tuple`.
* Triviallity of copy/move assignment of types is not preserved, just like `std::tuple`.
* The compiler is allowed to re-order elements captured in the lambda to minimize its size (by ordering elements by their `sizeof` or performing empty-class optimization). In practice, however, none of the major compilers seem to make use of it. ([godbolt](https://godbolt.org/z/Y5qbMe5Ge))
# Installation
Copy the single header from [src/ltpl/tuple.hpp](src/ltpl/tuple.hpp) into your project.
Alternatively, use CMake to install the project. From the root of the repository:
```cmake
cmake -B build -S .
cmake --install build --prefix build/out
```
And in your CMake project's CMakeLists.txt:
```cmake
# Add build/out to the CMAKE_PREFIX_PATH
find_package(lambda-tuple)
target_link_libraries(my_app PRIVATE lambda-tuple::lambda-tuple)
```
# Usage
Include the single header:
```cpp
#include
```
And use `ltpl::Tuple` just like `std::tuple`.
# Requirements
The only requirement is a small subset of C++20.
The following compilers are continuously tested by Github Actions:
* GCC 10,
* Clang 10
* MSVc 19.32
* AppleClang 13