NTuple
C++ does not have a built-in tuple-like container that can store a fixed-size tuple of values of the same type. This behavior can be achieved via manual typing
std::tuple<T, T, T, ...>whereTis the type of the values or viastd::array<T, N>whereNis the size of the tuple. I insist thatstd::array<T, N>is not semantically the same, so I wrote a customabl::ntuplecontainer that works the same way asabl::tuple.
Caution
Imports <concepts>, <type_traits>, <utility> and <cstdlib>.
Tip
To import use #include "atl/containers/ntuple.hpp".
Implementation details
The abl::ntuple container is implemented as a wrapper around a proxy struct that inherits from a pack of leaf structs, each of which holds a single value of the tuple. This makes the container occupy consecutive memory locations, allowing for efficient indexing and iteration via simple casts.
Constructors
abl::ntuple provides the following constructors:
abl::ntuple(): default constructor, initializes a tuple with default-constructed values.abl::ntuple(Args&&...): initializes the tuple with the given values.abl::ntuple(abl::ntuple const&): copy constructor, copies the tuple.abl::ntuple(abl::ntuple&&): move constructor, transfers ownership of the tuple.
Assignment operators
abl::ntuple provides the following assignment operators:
abl::ntuple& operator=(abl::ntuple const&): copy assignment operator, copies the tuple.abl::ntuple& operator=(abl::ntuple&&): move assignment operator, transfers ownership of the tuple.
Modifiers
abl::ntuple provides the following modifiers:
void swap(abl::ntuple&): swaps the contents of the tuple with another tuple.
Accessors
abl::ntuple provides the following accessors:
T& get<I>() &: returns a reference to the value at the given indexI.T const& get<I>() const &: returns a const reference to the value at the given indexI.T&& get<I>() &&: returns a rvalue reference to the value at the given indexI.T const&& get<I>() const &&: returns a const rvalue reference to the value at the given indexI.
Observers
abl::ntuple provides the following observers:
static std::size_t sizereturns the number of elements in the tuple.
Comparison operators
abl::ntuple provides the following comparison operators:
bool operator==(abl::ntuple const&): returnstrueif the tuples are equal,falseotherwise.bool operator<=>(abl::ntuple const&): a three-way comparison operator that returns0if the tuples are equal,1if the first tuple is greater, and-1if the second tuple is greater.
Non-member functions
abl::ntuple provides the following non-member functions:
void swap(abl::ntuple&, abl::ntuple&): swaps the contents of two tuples.T& get<I>(abl::ntuple&): returns a reference to the value at the given indexI.T const& get<I>(abl::ntuple const&): returns a const reference to the value at the given indexI.T&& get<I>(abl::ntuple&&): returns a rvalue reference to the value at the given indexI.T const&& get<I>(abl::ntuple const&&): returns a const rvalue reference to the value at the given indexI.
Note
Thus, abl::ntuple provides accessors in both standard functional style and as methods. Additionally, abl::ntuple implements the tuple-like interface, i.e. it allows unpacking. Check out the examples below.