KUMI v3.1.0
Exquisite Epidote
Loading...
Searching...
No Matches

◆ explicit()

template<typename... Ts>
template<typename... Us>
requires (sizeof...(Us) == sizeof...(Ts)) && (!std::same_as<tuple<Ts...>, tuple<Us...>>)
kumi::tuple< Ts >::explicit ( ! kumi_implementation_defined) const
inlinenodiscard

Enables static casting a tuple<Ts...> to a tuple<Us...>, the conversions is explicit if the casting requires internal explicit conversions.

Template Parameters
UsTypes composing the destination tuple
Note
This function does not participate in overload resolution if the target tuple cannot be constructed from the values of the source, if their size does not match or if they are the same type. The conversion is explicit if the conversion of some member to the target needs to be explicit.

This permits the conversion from a tuple<T>& to a tuple<T&> which makes it suitable for some zip-like cases such as building a structure of arrays iterator.

Example :

#include <kumi/kumi.hpp>
#include <iostream>
int main()
{
kumi::tuple a = { 65, 2.3, 4.5f};
auto b = static_cast<kumi::tuple<char,int,double>>(a);
std::cout << a << "\n";
std::cout << b << "\n";
}
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:33
#include <iostream>
#include <kumi/kumi.hpp>
#include <algorithm>
#include <ranges>
#include <vector>
template <class... Ts>
struct soa
{
struct iterator
{
using value_type = kumi::tuple<Ts...>;
soa* s;
std::size_t i;
auto operator==(iterator b) const -> bool { return i == b.i; }
auto operator<=>(iterator b) const { return i <=> b.i; }
auto operator++() ->iterator& { ++i; return *this; }
auto operator--() ->iterator& { --i; return *this; }
auto operator++(int) ->iterator { auto copy = *this; ++*this; return copy; }
auto operator--(int) ->iterator { auto copy = *this; --*this; return copy; }
friend auto operator+(iterator a, std::size_t b) -> iterator{ return {.s = a.s, .i = a.i + b}; }
friend auto operator+(std::size_t a, iterator b) -> iterator{ return {.s = b.s, .i = a + b.i}; }
friend auto operator-(iterator a, std::size_t b) -> iterator{ return {.s = a.s, .i = a.i - b}; }
friend auto operator-(std::size_t a, iterator b) -> iterator{ return {.s = b.s, .i = a - b.i}; }
auto operator+=(std::size_t b) -> iterator& { i += b; return *this; }
auto operator-=(std::size_t b) -> iterator& { i -= b; return *this; }
auto operator-(iterator b) const -> std::ptrdiff_t { return i - b.i; }
auto operator*() const
{
return kumi::apply([&](auto&... vs) { return kumi::tie(vs[i]...); }, s->fields);
}
auto operator[](std::ptrdiff_t b) const
{
return kumi::apply([&](auto&... vs) { return kumi::tie(vs[i + b]...); }, s->fields);
}
};
auto begin() -> iterator { return {.s = this, .i = 0 }; }
auto end() -> iterator { return {.s = this, .i = kumi::get<0>(fields).size() }; }
};
static_assert(std::random_access_iterator<soa<int>::iterator>);
int main()
{
soa<float,int> s;
get<0>(s.fields) = {1.2f,2.3f,3.4f,4.5f};
get<1>(s.fields) = {10,20,30,40};
for(auto e : s)
std::cout << e << "\n";
}
constexpr decltype(auto) apply(Function &&f, T &&t) noexcept(kumi_implementation_defined)
Invoke the Callable object f with the elements of the product type unrolled as arguments.
Definition apply.hpp:47
decltype(auto) constexpr get(record< Ts... > &r) noexcept
Extracts the Ith field from a kumi::record.
Definition record.hpp:604
friend constexpr auto operator==(tuple const &self, tuple< Us... > const &other) noexcept
Compares a tuple with an other for equality.
Definition tuple.hpp:352