https://github.com/lb--/tuples
C++1z template metaprogramming library for tuples as type vectors, in the public domain
https://github.com/lb--/tuples
Last synced: 9 months ago
JSON representation
C++1z template metaprogramming library for tuples as type vectors, in the public domain
- Host: GitHub
- URL: https://github.com/lb--/tuples
- Owner: LB--
- License: unlicense
- Created: 2015-09-17T07:37:10.000Z (almost 11 years ago)
- Default Branch: tuples
- Last Pushed: 2016-04-15T03:35:20.000Z (about 10 years ago)
- Last Synced: 2025-10-09T19:31:57.717Z (9 months ago)
- Language: CMake
- Homepage: https://github.com/LB--/tuples/releases
- Size: 40 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
tuples [](https://travis-ci.org/LB--/tuples)
======
Tuples are useful for acting as arrays of types in template metaprogramming, often called type vectors. This library has boilerplate code for dealing with tuples at compile-time and also some utility functions.
**Some support for C++1z is required.**
## Usage
### CMake
From the `cmake` directory, copy the `FindLB` directory to a place in your `CMAKE_MODULE_PATH`.
Then, add `find_package(LB/tuples 1 EXACT REQUIRED)` to your CMake script.
You may need to set the CMake variable `LB/tuples_ROOT` if you installed to a nonstandard location.
Finally, link to the `LB::tuples` imported target with `target_link_libraries()`.
### C++
Note that alongside `std::tuple` there is also [`LB::tuples::tuple`](https://github.com/LB--/tuples/blob/tuples/src/tuple.hpp) - the latter does not hold any data whereas the former does.
`std::tuple` does not support `void` or abstract types, whereas `LB::tuples::tuple` does - the latter should be your default.
#### [`tuple_template_forward`](https://github.com/LB--/tuples/blob/tuples/src/tuple_template_forward.hpp)
##### `#include `
Takes the types stored in an `LB::tuples::tuple` or a `std::tuple` and uses them as template parameters to the given template.
Due to limitations in the C++ language, you cannot forward types to a template that has value template arguments even if they are defaulted - instead, you should create a proxy template in that situation.
For [example](https://github.com/LB--/tuples/blob/tuples/test/tuple_template_forward.cpp), you could use it to convert between `LB::tuples::tuple` and `std::tuple`:
```cpp
//convert LB::tuples::tuple to std::tuple
using t1 = LB::tuples::tuple;
using t2 = LB::tuples::tuple_template_forward_t;
static_assert(std::is_same>{}, "t1 != t2");
//convert std::tuple to LB::tuples::tuple
using t3 = std::tuple;
using t4 = LB::tuples::tuple_template_forward_t;
static_assert(std::is_same>{}, "t3 != t4");
```
#### [`tuple_forward`](https://github.com/LB--/tuples/blob/tuples/src/tuple_forward.hpp)
##### `#include `
Takes the values in a `std::tuple` and calls a function with them.
See [`std::apply`](http://en.cppreference.com/w/cpp/utility/apply) - `tuple_forward` is an implementation for cases when `std::apply` is not available.
If you have access to `std::apply` and/or `std::invoke`, please use them instead as they are more robust.
When C++17 is finalized and major compilers and standard library implementations have support for `std::apply`, this function will be deprecated.
For obvious reasons, `LB::tuples::tuple` is not supported.
[Example](https://github.com/LB--/tuples/blob/tuples/test/tuple_forward.cpp):
```cpp
static constexpr auto t1 = std::make_tuple(1, 1);
static constexpr auto t2 = std::make_tuple(1, 2);
static_assert(LB::tuples::tuple_forward(std::equal_to{}, t1), "t1 doesn't contain the same value twice");
static_assert(!LB::tuples::tuple_forward(std::equal_to{}, t2), "t2 contains the same value twice");
```
#### [`tuple_type_cat`](https://github.com/LB--/tuples/blob/tuples/src/tuple_type_cat.hpp)
##### `#include `
Concatenates the types in zero or more `LB::tuples::tuple`s in left-to-right order.
`std::tuple` is not supported.
[Example](https://github.com/LB--/tuples/blob/tuples/test/tuple_type_cat.cpp):
```cpp
using t1 = LB::tuples::tuple;
using t2 = LB::tuples::tuple;
using t3 = typename LB::tuples::tuple_type_cat_t;
static_assert(std::is_same>{}, "t1 + t2 != t3");
```
#### [`tuple_contains`](https://github.com/LB--/tuples/blob/tuples/src/tuple_contains.hpp)
##### `#include `
Checks if an `LB::tuples::tuple` contains the given type.
`std::tuple` is not supported.
[Example](https://github.com/LB--/tuples/blob/tuples/test/tuple_contains.cpp):
```cpp
using t1 = LB::tuples::tuple;
using t2 = LB::tuples::tuple;
static_assert(!LB::tuples::tuple_contains{}, "t1 contains float");
static_assert(LB::tuples::tuple_contains_v, "t2 doesn't contain float");
```
#### [`tuple_prune`](https://github.com/LB--/tuples/blob/tuples/src/tuple_prune.hpp)
##### `#include `
Takes an `LB::tuples::tuple` and removes duplicate types without rearranging the order of the types.
`std::tuple` is not supported.
[Example](https://github.com/LB--/tuples/blob/tuples/test/tuple_prune.cpp):
```cpp
using pruned_t = LB::tuples::tuple_prune_t
<
LB::tuples::tuple
>;
static_assert(std::is_same>{}, "tuple_prune is broken");
```
#### [`multi_assert`](https://github.com/LB--/tuples/blob/tuples/src/multi_assert.hpp)
##### `#include `
Given a template that takes zero or more template arguments and yields a `value` member, takes all the given `LB::tuples::tuple`s and/or `std::tuple`s and forwards their contents to the given template individually.
The resulting `value` is all the intermediate `value`s combined with `&&`.
In other words, asserts that multiple tuples satisfy a requirement.
[Example](https://github.com/LB--/tuples/blob/tuples/test/multi_assert.cpp):
```cpp
using t1 = LB::tuples::tuple;
using t2 = LB::tuples::tuple;
using t3 = LB::tuples::tuple;
static_assert(LB::tuples::multi_assert{}, "t1 or t2 isn't homogeneous");
static_assert(!LB::tuples::multi_assert_v, "t1, t2 and t3 are homogeneous");
```