VCCC  2024.05
VisualCamp Common C++ library
tuple

Implementation of STL header <tuple> More...

Detailed Description

See tuple for more information.

Classes

struct  tuple_like< T >
 specifies that a type implemented the tuple protocol More...
 
struct  sized_tuple_like< T, N >
 check if tuple-like objects with exactly N elements. More...
 

Typedefs

template<typename T >
using pair_like = sized_tuple_like< T, 2 >
 pair-like objects are tuple-like objects with exactly 2 elements. More...
 

Functions

template<class F , class Tuple >
constexpr decltype(auto) apply (F &&f, Tuple &&t)
 calls a function with the elements of tuple of arguments More...
 
template<typename T , typename Tuple , std::enable_if_t< internal::is_constructible_from_tuple< T, Tuple >::value, int > = 0>
constexpr T make_from_tuple (Tuple &&t)
 construct an object with a tuple of arguments More...
 
template<typename Tuple , typename T , typename F , std::enable_if_t< conjunction< tuple_like< Tuple >>::value, int > = 0>
constexpr auto tuple_fold_left (Tuple &&tuple, T &&init, F &&f)
 Left fold operation for each tuple elements. More...
 
template<typename Tuple , typename F >
constexpr std::enable_if_t< detail::tuple_for_each_invocable< Tuple, F >::valuetuple_for_each (Tuple &&t, F &&f)
 
template<typename Tuple , typename F >
constexpr std::enable_if_t< detail::tuple_for_each_in_place_index_invocable< Tuple, F >::valuetuple_for_each_index (Tuple &&t, F &&f)
 
template<typename Tuple , typename F >
constexpr auto tuple_transform (Tuple &&t, F &&f) noexcept(noexcept(detail::tuple_transform_impl(std::forward< Tuple >(t), std::forward< F >(f), std::make_index_sequence< std::tuple_size< remove_cvref_t< Tuple >>::value >{})))
 Constructs a new tuple with each elements transformed. More...
 

Typedef Documentation

◆ pair_like

using pair_like = sized_tuple_like<T, 2>
See Also
tuple-like : specifies that a type implemented the tuple protocol

Function Documentation

◆ apply()

constexpr decltype(auto) vccc::apply ( F &&  f,
Tuple &&  t 
)
inlineconstexpr

Invoke the Callable object f with the elements of t as arguments.

Parameters
fCallable object to be invoked
ttuple whose elements to be used as arguments to f
Returns
The value returned by f.
Note
Tuple need not be std::tuple, and instead may be anything that supports std::get and std::tuple_size; in particular, std::array and std::pair may be used.
Example
#include <iostream>
#include <tuple>
#include <utility>
#include "vccc/tuple.hpp"
int add(int first, int second) { return first + second; }
template<typename T>
T add_generic(T first, T second) { return first + second; }
auto add_lambda = [](auto first, auto second) { return first + second; };
template<typename... Ts>
std::ostream& operator<<(std::ostream& os, std::tuple<Ts...> const& theTuple)
{
(
[&os](Ts const&... tupleArgs)
{
os << '[';
std::size_t n{0};
((os << tupleArgs << (++n != sizeof...(Ts) ? ", " : "")), ...);
os << ']';
}, theTuple
);
return os;
}
int main()
{
// OK
std::cout << vccc::apply(add, std::pair(1, 2)) << '\n';
// Error: can't deduce the function type
// std::cout << vccc::apply(add_generic, std::make_pair(2.0f, 3.0f)) << '\n';
// OK
std::cout << vccc::apply(add_lambda, std::pair(2.0f, 3.0f)) << '\n';
// advanced example
std::tuple myTuple{25, "Hello", 9.31f, 'c'};
std::cout << myTuple << '\n';
}
std::ostream & operator<<(std::ostream &os, const MatrixBase< E > &mat_expr)
Definition: matrix_ostream.hpp:17
constexpr decltype(auto) apply(F &&f, Tuple &&t)
calls a function with the elements of tuple of arguments
Definition: apply.hpp:89
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > second
Definition: key_value.hpp:36
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 0 > first
Definition: key_value.hpp:34
cv::Matx< T, m, n > add(const cv::Matx< T, m, n > &matx, N n_)
Definition: cv_mat.hpp:150

Output:

3
5
[25, Hello, 9.31, c]

◆ make_from_tuple()

constexpr T vccc::make_from_tuple ( Tuple &&  t)
constexpr

Construct an object of type T, using the elements of the tuple t as the arguments to the constructor.

Parameters
ttuple whose elements to be used as arguments to the constructor of T
Returns
The constructed T object or reference.
Example
#include <iostream>
#include <tuple>
#include "vccc/tuple.hpp"
struct Foo
{
Foo(int first, float second, int third)
{
std::cout << first << ", " << second << ", " << third << '\n';
}
};
int main()
{
auto tuple = std::make_tuple(42, 3.14f, 0);
vccc::make_from_tuple<Foo>(std::move(tuple));
}

Output:

42, 3.14, 0

◆ tuple_fold_left()

constexpr auto vccc::tuple_fold_left ( Tuple &&  tuple,
T &&  init,
F &&  f 
)
constexpr

◆ tuple_for_each()

constexpr std::enable_if_t<detail::tuple_for_each_invocable<Tuple, F>::value> vccc::tuple_for_each ( Tuple &&  t,
F &&  f 
)
constexpr

◆ tuple_for_each_index()

constexpr std::enable_if_t<detail::tuple_for_each_in_place_index_invocable<Tuple, F>::value> vccc::tuple_for_each_index ( Tuple &&  t,
F &&  f 
)
constexpr

◆ tuple_transform()

constexpr auto vccc::tuple_transform ( Tuple &&  t,
F &&  f 
)
constexprnoexcept