WPILibC++ 2024.3.2
array.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <array>
8#include <concepts>
9#include <cstddef>
10#include <tuple>
11#include <utility>
12
13namespace wpi {
14
15struct empty_array_t {};
16constexpr empty_array_t empty_array;
17
18/**
19 * This class is a wrapper around std::array that does compile time size
20 * checking.
21 *
22 * std::array's implicit constructor can result in uninitialized elements if the
23 * number of arguments doesn't match the std::array size.
24 */
25template <typename T, size_t N>
26class array : public std::array<T, N> {
27 public:
28 constexpr explicit array(empty_array_t) {}
29
30 template <std::convertible_to<T>... Ts>
31 requires(1 + sizeof...(Ts) == N)
32 constexpr array(T arg, Ts&&... args) // NOLINT
33 : std::array<T, N>{std::forward<T>(arg), std::forward<Ts>(args)...} {}
34
35 constexpr array(const array<T, N>&) = default;
36 constexpr array& operator=(const array<T, N>&) = default;
37 constexpr array(array<T, N>&&) = default;
38 constexpr array& operator=(array<T, N>&&) = default;
39
40 constexpr array(const std::array<T, N>& rhs) { // NOLINT
41 *static_cast<std::array<T, N>*>(this) = rhs;
42 }
43
44 constexpr array& operator=(const std::array<T, N>& rhs) {
45 *static_cast<std::array<T, N>*>(this) = rhs;
46 return *this;
47 }
48
49 constexpr array(std::array<T, N>&& rhs) { // NOLINT
50 *static_cast<std::array<T, N>*>(this) = rhs;
51 }
52
53 constexpr array& operator=(std::array<T, N>&& rhs) {
54 *static_cast<std::array<T, N>*>(this) = rhs;
55 return *this;
56 }
57};
58
59template <typename T, std::convertible_to<T>... Ts>
60array(T, Ts...) -> array<T, 1 + sizeof...(Ts)>;
61
62} // namespace wpi
63
64template <size_t I, typename T, size_t N>
65 requires(I < N)
66constexpr T& get(wpi::array<T, N>& arr) noexcept {
67 return std::get<I>(static_cast<std::array<T, N>>(arr));
68}
69
70template <size_t I, typename T, size_t N>
71 requires(I < N)
72constexpr T&& get(wpi::array<T, N>&& arr) noexcept {
73 return std::move(std::get<I>(arr));
74}
75
76template <size_t I, typename T, size_t N>
77 requires(I < N)
78constexpr const T& get(const wpi::array<T, N>& arr) noexcept {
79 return std::get<I>(static_cast<std::array<T, N>>(arr));
80}
81
82template <size_t I, typename T, size_t N>
83 requires(I < N)
84constexpr const T&& get(const wpi::array<T, N>&& arr) noexcept {
85 return std::move(std::get<I>(arr));
86}
87
88// Enables structured bindings
89namespace std { // NOLINT
90// Partial specialization for wpi::array
91template <typename T, size_t N>
92struct tuple_size<wpi::array<T, N>> : public integral_constant<size_t, N> {};
93
94// Partial specialization for wpi::array
95template <size_t I, typename T, size_t N>
96 requires(I < N)
97struct tuple_element<I, wpi::array<T, N>> {
98 using type = T;
99};
100} // namespace std
constexpr T & get(wpi::array< T, N > &arr) noexcept
Definition: array.h:66
This class is a wrapper around std::array that does compile time size checking.
Definition: array.h:26
constexpr array & operator=(std::array< T, N > &&rhs)
Definition: array.h:53
constexpr array & operator=(const std::array< T, N > &rhs)
Definition: array.h:44
constexpr array & operator=(const array< T, N > &)=default
constexpr array(const std::array< T, N > &rhs)
Definition: array.h:40
constexpr array(std::array< T, N > &&rhs)
Definition: array.h:49
constexpr array(const array< T, N > &)=default
constexpr array(array< T, N > &&)=default
constexpr array & operator=(array< T, N > &&)=default
constexpr array(empty_array_t)
Definition: array.h:28
constexpr array(T arg, Ts &&... args)
Definition: array.h:32
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
\rst Returns a named argument to be used in a formatting function.
Definition: core.h:1841
Definition: array.h:89
Definition: ntcore_cpp.h:26
array(T, Ts...) -> array< T, 1+sizeof...(Ts)>
Definition: array.h:15
constexpr empty_array_t empty_array
Definition: array.h:16