KUMI v5.0.0
Gorgeous Garnet
Loading...
Searching...
No Matches

Fixed-size collection of homogeneous values. More...

#include <kumi/product_types/array.hpp>

Detailed Description

template<typename T, std::size_t N>
class kumi::array< T, N >

Fixed-size collection of homogeneous values.

kumi::array is different from std::array in several aspects. It can hold reference type and it's possible to create an empty kumi::array<> that deduces to kumi::array<void,0>. This is the proper type representation of the identity of power types.

When passed into algorithms, kumi::array can decay to it's more general case that is kumi::tuple if at any point the algorithms would generate heterogeneous types.

Note
It is possible to defined a kumi::array<T> which deduces to an array of type T with arity 0.

kumi::array provides an aggregate based implementation of an array. It provides algorithms and functions designed to facilitate array's handling and transformations.

kumi::array is also compatible with standard tuple operations and structured bindings.

Template Parameters
TType stored inside kumi::array.
NSize of the sequence

Example:

#include <iostream>
#include <stdexcept>
#include <string>
#include <kumi/kumi.hpp>
{
switch (id)
{
case 0: return {3.8, 'A', "Lisa Simpson"};
case 1: return {2.9, 'C', "Milhouse Van Houten"};
case 2: return {1.7, 'D', "Ralph Wiggum"};
case 3: return {0.6, 'F', "Bart Simpson"};
}
throw std::invalid_argument("id");
}
int main()
{
const auto student0 = get_student(0);
std::cout << "ID: 0, "
<< "GPA: " << kumi::get<0>(student0) << ", "
<< "grade: " << kumi::get<1>(student0) << ", "
<< "name: " << kumi::get<2>(student0) << '\n';
const auto student1 = get_student(1);
std::cout << "ID: 1, "
<< "GPA: " << kumi::get<double>(student1) << ", "
<< "grade: " << kumi::get<char>(student1) << ", "
<< "name: " << kumi::get<std::string>(student1) << '\n';
double gpa2;
char grade2;
std::string name2;
kumi::tie(gpa2, grade2, name2) = get_student(2);
std::cout << "ID: 2, "
<< "GPA: " << gpa2 << ", "
<< "grade: " << grade2 << ", "
<< "name: " << name2 << '\n';
// C++17 structured binding:
const auto [gpa3, grade3, name3] = get_student(3);
std::cout << "ID: 3, "
<< "GPA: " << gpa3 << ", "
<< "grade: " << grade3 << ", "
<< "name: " << name3 << '\n';
}
constexpr tie_t tie
Definition tuple.hpp:540
decltype(auto) constexpr get(array< T, N > &t) noexcept
Extracts the Ith element from a kumi::array.
Definition array.hpp:672
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:35

Public Member Functions

template<typename U>
requires (std::is_assignable_v<T&, U&&>)
constexpr array & operator= (array< U, N > &&other) noexcept(std::is_nothrow_assignable_v< T &, U && >)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename U>
requires (std::is_assignable_v<T&, U const&>)
constexpr array & operator= (array< U, N > const &other) noexcept(std::is_nothrow_assignable_v< T &, U const & >)
 Replaces the contents of the array with the contents of another array.
template<std::size_t I>
requires (I < N)
constexpr decltype(auto) operator[] (kumi::index_t< I > i) &noexcept
 Extracts the Ith element from a kumi::array.
template<std::size_t I>
requires (I < N)
constexpr decltype(auto) operator[] (kumi::index_t< I >) &&noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<std::size_t I>
requires (I < N)
constexpr decltype(auto) operator[] (kumi::index_t< I >) const &&noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<std::size_t I>
requires (I < N)
constexpr decltype(auto) operator[] (kumi::index_t< I >) const &noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Conversions
template<typename U>
 explicit (!std::convertible_to< T const &, U >) const expr operator array< U
 Enables static casting a array<T,N> to an array<U,N>, the conversions is explicit if the casting requires internal explicit conversions.
template<typename U>
 explicit (!std::convertible_to< T &, U >) const expr operator array< U
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Friends

constexpr void swap (array &a, array &b) noexcept(std::is_nothrow_swappable_v< T >)
 Swaps the contents of two kumi::arrays.

(Note that these are not member symbols.)

template<typename CharT, typename Traits, typename T, std::size_t N>
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, array< T, N > const &a) noexcept
 Inserts a kumi::array in an output stream.
template<typename T, typename U, std::size_t N>
constexpr auto operator== (array< T, N > const &self, array< U, N > const &other) noexcept
 Compares an array with another for equality.
template<typename T, typename U, std::size_t N>
constexpr auto operator!= (array< T, N > const &self, array< U, N > const &other) noexcept
 Compares an array with another for inequality.
template<typename T, typename U, std::size_t N>
constexpr auto operator< (array< T, N > const &lhs, array< U, N > const &rhs) noexcept
 Compares arrays for the lexicographical less-than relation.
template<typename T, typename U, std::size_t N>
constexpr auto operator<= (array< T, N > const &lhs, array< U, N > const &rhs) noexcept
 Compares arrays for the lexicographical less-than-or-equal relation.
template<typename T, typename U, std::size_t N>
constexpr auto operator> (array< T, N > const &lhs, array< U, N > const &rhs) noexcept
 Compares arrays for the lexicographical greater-than relation.
template<typename T, typename U, std::size_t N>
constexpr auto operator>= (array< T, N > const &lhs, array< U, N > const &rhs) noexcept
 Compares arrays for the lexicographical greater-than-or-equal relation.
template<typename T, typename... Ts>
 array (T &&, Ts &&...) -> array< std::unwrap_ref_decay_t< T >, sizeof...(Ts)+1 >
 kumi::array deduction guide
template<std::size_t I, typename T, std::size_t N>
decltype(auto) constexpr get (array< T, N > &t) noexcept
 Extracts the Ith element from a kumi::array.
template<std::size_t I, typename T, std::size_t N>
decltype(auto) constexpr get (array< T, N > &&arg) noexcept
template<std::size_t I, typename T, std::size_t N>
decltype(auto) constexpr get (array< T, N > const &arg) noexcept
template<std::size_t I, typename T, std::size_t N>
decltype(auto) constexpr get (array< T, N > const &&arg) noexcept

Member Function Documentation

◆ explicit()

template<typename T, std::size_t N>
template<typename U>
kumi::array< T, N >::explicit ( !std::convertible_to< T const &, U > ) const
inlineexplicit

Enables static casting a array<T,N> to an array<U,N>, the conversions is explicit if the casting requires internal explicit conversions.

Template Parameters
UType of the elements of the destination array
Note
This function does not participate in overload resolution if the target array 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 an array<T>& to an array<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::array a = { 65, 2, 4 };
auto b = static_cast<kumi::array<char, 3>>(a);
std::cout << a << "\n";
std::cout << b << "\n";
}
Fixed-size collection of homogeneous values.
Definition array.hpp:43

◆ operator=()

template<typename T, std::size_t N>
template<typename U>
requires (std::is_assignable_v<T&, U const&>)
array & kumi::array< T, N >::operator= ( array< U, N > const & other)
inlineconstexprnoexcept

Replaces the contents of the array with the contents of another array.

Parameters
otherkumi::array to copy or move from
Returns
*this
Note
This function does not participate in overload resolution if the members from the source array cannot be converted to the members of the current array.

◆ operator[]()

template<typename T, std::size_t N>
template<std::size_t I>
requires (I < N)
decltype(auto) kumi::array< T, N >::operator[] ( kumi::index_t< I > i) &
inlineconstexprnoexcept

Extracts the Ith element from a kumi::array.

Note
Does not participate in overload resolution if I is not in [0, N).
Parameters
iCompile-time index of the element to access
Returns
A reference to the selected element of current array.

Example:

#include <kumi/kumi.hpp>
#include <iostream>
int main()
{
using namespace kumi::literals;
kumi::array a = { 1, 2, 3, 4 };
std::cout << a[0_c] << "\n";
std::cout << a[1_c] << "\n";
std::cout << a[2_c] << "\n";
a[3_c]++;
std::cout << a[kumi::index<3>] << "\n";
}

◆ array()

template<typename T, typename... Ts>
array ( T && ,
Ts && ... ) -> array< std::unwrap_ref_decay_t< T >, sizeof...(Ts)+1 >
related

kumi::array deduction guide

Template Parameters
TsType lists to build the array with.

◆ get() [1/4]

template<std::size_t I, typename T, std::size_t N>
decltype(auto) constexpr get ( array< T, N > && arg)
related

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ get() [2/4]

template<std::size_t I, typename T, std::size_t N>
decltype(auto) constexpr get ( array< T, N > & t)
relatednodiscardinlineconstexprnoexcept

Extracts the Ith element from a kumi::array.

Note
Does not participate in overload resolution if I is not in [0, sizeof...(Ts)).
Template Parameters
ICompile-time index of the element to access
Parameters
tarray to index
Returns
A reference to the selected element of t.

Example:

#include <kumi/kumi.hpp>
#include <iostream>
int main()
{
kumi::array a = { 1, 2, 3, 4 };
std::cout << kumi::get<0>(a) << "\n";
std::cout << kumi::get<1>(a) << "\n";
std::cout << kumi::get<2>(a) << "\n";
std::cout << kumi::get<3>(a) << "\n";
}

◆ get() [3/4]

template<std::size_t I, typename T, std::size_t N>
decltype(auto) constexpr get ( array< T, N > const && arg)
related

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ get() [4/4]

template<std::size_t I, typename T, std::size_t N>
decltype(auto) constexpr get ( array< T, N > const & arg)
related

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ operator<()

template<typename T, typename U, std::size_t N>
auto operator< ( array< T, N > const & lhs,
array< U, N > const & rhs )
related

Compares arrays for the lexicographical less-than relation.

Note
This function does not participate in overload resolution if the arrays are not lexicographically ordered

◆ swap

template<typename T, std::size_t N>
void swap ( array< T, N > & a,
array< T, N > & b )
friend

Swaps the contents of two kumi::arrays.

See also
Callable with the usual using std::swap; swap(a, b) idiom, exchanges every element through ADL and does not participate in overload resolution if an element is not swappable.