Tuple¶
The header file tuple provides utilities for manipulating tuples, including moving, copying, and swapping elements between tuples. It also allows applying functions to corresponding elements of multiple tuples. Additionally, it offers a class for managing tuples of pointers.
Defines
-
COMPARE_OPERATOR(op)¶
Macro to define comparison operators for pointer_tuple.
This macro defines a friend function for the specified comparison operator to compare two pointer_tuple objects.
- Parameters:
op – The comparison operator to define (e.g., ==, !=, <, >, <=, >=).
-
SWAP_REFERENCES(lhs_ref, rhs_ref)¶
Macro to define swap functions for pointer_tuple.
This macro defines a template function to swap the data between two pointer_tuple objects with different reference qualifiers.
- Parameters:
lhs_ref – The reference qualifier for the left-hand side pointer_tuple.
rhs_ref – The reference qualifier for the right-hand side pointer_tuple.
-
template<typename ...Ts>
struct tuple_size<utils::pointer_tuple<Ts...>>¶
-
template<size_t I, typename ...Ts>
struct tuple_element<I, utils::pointer_tuple<Ts...>>¶
-
namespace utils
Functions
-
template<std::size_t I = 0, typename ...Fs, typename ...Ts>
void move_tuple_elements(std::tuple<Fs...> &from, std::tuple<Ts...> &to)¶ Moves elements from one tuple to another.
This function template moves elements from the
from
tuple to theto
tuple. It uses compile-time recursion to iterate over the elements of the tuples.Remark
If at least one of the corresponding elements are not move assignable, the function will not compile.
Note
The function moves first
min(sizeof...(Fs), sizeof...(Ts))
elements.- Template Parameters:
I – The current index in the tuple (default is 0).
Fs – The types of the elements in the
from
tuple.Ts – The types of the elements in the
to
tuple.
- Parameters:
from – The source tuple from which elements are moved.
to – The destination tuple to which elements are moved.
-
template<std::size_t I = 0, typename ...Ts, typename ...Us>
void swap_tuple_elements(std::tuple<Ts...> &tuple1, std::tuple<Us...> &tuple2)¶ Swaps elements between two tuples.
This function template swaps elements between the
tuple1
andtuple2
tuples. It uses compile-time recursion to iterate over the elements of the tuples.Remark
If at least one of the corresponding elements are not swappable, the function will not compile.
Note
The function swaps first
min(sizeof...(Ts), sizeof...(Us))
elements.- Template Parameters:
I – The current index in the tuple (default is 0).
Ts – The types of the elements in the
tuple1
.Us – The types of the elements in the
tuple2
.
- Parameters:
tuple1 – The first tuple whose elements are to be swapped.
tuple2 – The second tuple whose elements are to be swapped.
-
template<std::size_t I = 0, typename ...Fs, typename ...Ts>
void copy_tuple_elements(const std::tuple<Fs...> &from, std::tuple<Ts...> &to)¶ Copies elements from one tuple to another.
This function template copies elements from the
from
tuple to theto
tuple. It uses compile-time recursion to iterate over the elements of the tuples.Remark
If at least one of the corresponding elements is not copy assignable, the function will not compile.
Note
The function copies the first
min(sizeof...(Fs), sizeof...(Ts))
elements.- Template Parameters:
I – The current index in the tuple (default is 0).
Fs – The types of the elements in the
from
tuple.Ts – The types of the elements in the
to
tuple.
- Parameters:
from – The source tuple from which elements are copied.
to – The destination tuple to which elements are copied.
-
template<typename F, typename ...Tuples>
void constexpr_for_tuples(F &&f, Tuples&&... tuples)¶ Applies a function to corresponding elements of multiple tuples.
This function template applies the given function
f
to the corresponding elements of the provided tuples. It uses compile-time recursion to iterate over the elements of the tuples.- Template Parameters:
F – The type of the function to apply.
Tuples – The types of the tuples.
- Parameters:
f – The function to apply to the elements of the tuples.
tuples – The tuples whose elements are to be processed.
-
template<std::size_t N, typename ...InputIt>
std::tuple_element_t<N, pointer_tuple<InputIt...>> get(pointer_tuple<InputIt...> &ptr_tuple)¶ Returns a reference to the N-th element in the tuple of pointers.
This function template returns a reference to the N-th element in the tuple of pointers.
- Template Parameters:
N – The index of the element to return.
InputIt – The types of the elements in the pointer_tuple.
- Parameters:
ptr_tuple – The reference to pointer_tuple object whose element is to be returned.
- Returns:
A reference to the N-th element in the tuple of pointers.
-
template<std::size_t N, typename ...InputIt>
std::tuple_element_t<N, pointer_tuple<InputIt...>> get(pointer_tuple<InputIt...> &&ptr_tuple)¶ Returns a reference to the N-th element in the tuple of pointers.
This function template returns a reference to the N-th element in the tuple of pointers.
- Template Parameters:
N – The index of the element to return.
InputIt – The types of the elements in the pointer_tuple.
- Parameters:
ptr_tuple – The rvalue pointer_tuple object whose element is to be returned.
- Returns:
A reference to the N-th element in the tuple of pointers.
-
template<std::size_t N, typename ...Ts>
std::tuple_element_t<N, pointer_tuple<Ts...>> get(const pointer_tuple<Ts...> &ptr_tuple)¶ Returns a reference to the N-th element in the tuple of pointers.
This function template returns a reference to the N-th element in the tuple of pointers. It is a const version of the get function.
- Template Parameters:
N – The index of the element to return.
Ts – The types of the elements in the pointer_tuple.
- Parameters:
ptr_tuple – The const reference to a pointer_tuple object whose element is to return.
- Returns:
A reference to the N-th element in the tuple of pointers.
-
template<std::size_t N, typename ...Ts>
decltype(auto) get(const std::tuple<Ts...> &tuple)¶ Returns a reference to the N-th element in the tuple.
- Parameters:
tuple – The const reference to the std::tuple object whose element is to be returned.
-
template<std::size_t N, typename ...Ts>
decltype(auto) get(std::tuple<Ts...> &&tuple)¶ Returns a reference to the N-th element in the tuple.
- Parameters:
tuple – The rvalue reference to the std::tuple object whose element is to be returned.
-
template<typename CharT, typename Traits, typename ...Ts, std::enable_if_t<(sizeof...(Ts) > 0), bool> = true>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, pointer_tuple<Ts...> &ptr_tuple)¶ Overload of the stream insertion operator for reference pointer_tuple.
This function template overloads the stream insertion operator (<<) for pointer_tuple objects. It formats the elements pointed to by the pointers in the tuple and inserts them into the output stream.
- Template Parameters:
CharT – The character type of the output stream.
Traits – The traits type of the output stream.
Ts – The types of the elements in the pointer_tuple.
- Parameters:
os – The output stream to which the formatted string is inserted.
ptr_tuple – The pointer_tuple object whose elements are to be formatted and inserted into the output stream.
- Returns:
A reference to the output stream.
-
template<typename ...Ts>
class pointer_tuple¶ - #include <tuple.hpp>
A class that holds a tuple of pointers to elements.
This class template holds a tuple of pointers to objects of type
Ts...
. Move and copy operators are applied to the objects pointed by the pointers but not to the tuple itself. That is: the addresses of the pointers remain the same after operations. Thought class manages the pointers,get
function and structured bindings return references.- Template Parameters:
Ts – The types of the elements.
Public Functions
-
pointer_tuple() = delete¶
-
pointer_tuple(const pointer_tuple &other) = default¶
Copy constructor.
This constructor creates a new pointer_tuple object as a copy of an existing pointer_tuple object. It performs a kind of shallow copy. That is: the pointers are not the references, but they point to the same objects.
- Parameters:
other – The pointer_tuple object to copy from.
-
pointer_tuple(pointer_tuple &&other) = default¶
Move constructor.
This constructor creates a new pointer_tuple object by moving the contents of an existing pointer_tuple object.
- Parameters:
other – The pointer_tuple object to move from.
-
~pointer_tuple() = default¶
-
inline explicit pointer_tuple(Ts*... pointers)¶
Constructs a pointer_tuple object with the given pointers of elements.
- Parameters:
pointers – The elements to be stored in the tuple.
-
inline pointer_tuple &operator=(const pointer_tuple &other)¶
Copy assignment operator.
Copies the objects pointed to by the pointers from the other pointer_tuple object to the objects pointed to by the pointers in this object.
- Parameters:
other – The pointer_tuple object to copy from.
- Returns:
A reference to this object.
-
inline pointer_tuple &operator=(pointer_tuple &&other) noexcept¶
Move assignment operator.
Moves the objects pointed to by the pointers from the other pointer_tuple object to the objects pointed to by the pointers in this object.
- Parameters:
other – The pointer_tuple object to move from.
- Returns:
A reference to this object.
-
inline pointer_tuple &operator=(const std::tuple<Ts...> &values)¶
Assignment operator for pointer_tuple.
This operator assigns the values from a given std::tuple to the elements pointed to by the pointers in the pointer_tuple.
- Parameters:
values – A std::tuple containing the values to be assigned.
- Returns:
A reference to the modified pointer_tuple.
-
inline operator std::tuple<Ts...>() const¶
Converts the pointer_tuple to a std::tuple.
This operator converts the pointer_tuple object to a std::tuple containing references to the elements pointed to by the pointers in the tuple.
- Returns:
A std::tuple containing references to the elements.
-
inline void swapData(pointer_tuple &o)¶
Swaps the data between this pointer_tuple and another.
This function swaps the elements between the tuple of pointers in this pointer_tuple object and the tuple of pointers in the provided pointer_tuple object.
- Parameters:
o – The pointer_tuple object to swap data with.
Friends
-
template<std::size_t N, typename ...InputIt>
friend std::tuple_element_t<N, pointer_tuple<InputIt...>> get(pointer_tuple<InputIt...> &ptr_tuple)¶ Returns a reference to the N-th element in the tuple of pointers.
This function template returns a reference to the N-th element in the tuple of pointers.
- Template Parameters:
N – The index of the element to return.
InputIt – The types of the elements in the pointer_tuple.
- Parameters:
ptr_tuple – The reference to pointer_tuple object whose element is to be returned.
- Returns:
A reference to the N-th element in the tuple of pointers.
-
template<std::size_t N, typename ...InputIt>
friend std::tuple_element_t<N, pointer_tuple<InputIt...>> get(pointer_tuple<InputIt...> &&ptr_tuple)¶ Returns a reference to the N-th element in the tuple of pointers.
This function template returns a reference to the N-th element in the tuple of pointers.
- Template Parameters:
N – The index of the element to return.
InputIt – The types of the elements in the pointer_tuple.
- Parameters:
ptr_tuple – The rvalue pointer_tuple object whose element is to be returned.
- Returns:
A reference to the N-th element in the tuple of pointers.
-
template<std::size_t N, typename ...InputIt>
friend std::tuple_element_t<N, pointer_tuple<InputIt...>> get(const pointer_tuple<InputIt...> &ptr_tuple)¶
-
template<std::size_t I = 0, typename ...Fs, typename ...Ts>
-
namespace std¶
- template<size_t I, typename... Ts> pointer_tuple< Ts... > >
- template<typename... Ts> pointer_tuple< Ts... > >
Public Static Attributes
-
static int value = sizeof...(Ts)¶
-
static int value = sizeof...(Ts)¶
Usage¶
The following examples demonstrate how to use the tuple header file:
move_tuple_elements
Output:
1 2 3 4 5
swap_tuple_elements
d::vector vec1{1, 2, 3, 4, 5};
to tuple1{std::forward_as_tuple(vec1)};
d::vector vec2{6, 7, 8, 9, 10, 11, 12};
to tuple2{std::forward_as_tuple(vec2)};
utils::swap_tuple_elements(tuple1, tuple2);
for (const auto & e : vec1) {
std::cout << e << " ";
}
Output:
6 7 8 9 10 11 12
copy_tuple_elements
d::vector vec1{1, 2, 3, 4, 5};
to from{std::forward_as_tuple(vec1)};
d::vector vec2{6, 7, 8, 9, 10, 11, 12};
to to{std::forward_as_tuple(vec2)};
utils::copy_tuple_elements(from, to);
for (const auto & e : vec2) {
std::cout << e << " ";
}
Output:
1 2 3 4 5
constexpr_for_tuples
d::tuple tuple1(1, 2.0, 'a');
d::tuple tuple2(3L, 4.0f, 'b');
d::vector<int> results;
ils::constexpr_for_tuples([&](auto a, auto b) { results.push_back(a + b); },
tuple1, tuple2);
for (const auto & e : results) {
std::cout << e << " ";
}
Output:
4, 6, 195
PointerTuple
Please see the test file test.tuple.cpp
for an example of how to use
the PointerTuple
class.