Files
boost_array/doc/array/reference.adoc

406 lines
8.0 KiB
Plaintext
Raw Normal View History

2024-12-18 15:31:13 -08:00
////
Copyright 2001-2004 Nicolai M. Josuttis
Copyright 2012 Marshall Clow
Copyright 2024 Christian Mazakas
Distributed under the Boost Software License, Version 1.0.
https://www.boost.org/LICENSE_1_0.txt
////
[#reference]
# Reference
:idprefix: reference_
:cpp: C++
## Header <boost/array.hpp>
```cpp
namespace boost {
2025-01-25 18:00:27 +02:00
2024-12-18 15:31:13 -08:00
template<typename T, std::size_t N> class array;
2025-01-25 18:00:27 +02:00
template<typename T, std::size_t N>
constexpr void swap(array<T, N>&, array<T, N>&);
2024-12-18 15:31:13 -08:00
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator==(const array<T, N>&, const array<T, N>&);
2024-12-18 15:31:13 -08:00
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator!=(const array<T, N>&, const array<T, N>&);
2024-12-18 15:31:13 -08:00
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator<(const array<T, N>&, const array<T, N>&);
2024-12-18 15:31:13 -08:00
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator>(const array<T, N>&, const array<T, N>&);
2024-12-18 15:31:13 -08:00
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator<=(const array<T, N>&, const array<T, N>&);
2024-12-18 15:31:13 -08:00
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator>=(const array<T, N>&, const array<T, N>&);
template<std::size_t Idx, typename T, std::size_t N>
constexpr T& get(array<T, N>&) noexcept;
template<std::size_t Idx, typename T, std::size_t N>
constexpr const T& get(const array<T, N>&) noexcept;
2024-12-18 15:31:13 -08:00
}
```
## Class template array
### Synopsis
```cpp
// In header: <boost/array.hpp>
template<typename T, std::size_t N>
class array {
public:
2025-01-25 18:00:27 +02:00
2024-12-18 15:31:13 -08:00
// types
2025-01-25 18:00:27 +02:00
2024-12-18 15:31:13 -08:00
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// static constants
2025-01-25 18:00:27 +02:00
2024-12-18 15:31:13 -08:00
static const size_type static_size = N;
2025-01-25 18:00:27 +02:00
// construct/copy/destroy
2024-12-18 15:31:13 -08:00
template<typename U> array& operator=(const array<U, N>&);
// iterator support
2025-01-25 18:00:27 +02:00
constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
constexpr const_iterator cbegin() const noexcept;
constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;
2024-12-18 15:31:13 -08:00
// reverse iterator support
2025-01-25 18:00:27 +02:00
reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator crbegin() const noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crend() const noexcept;
2024-12-18 15:31:13 -08:00
// capacity
2025-01-25 18:00:27 +02:00
static constexpr size_type size() noexcept;
static constexpr bool empty() noexcept;
static constexpr size_type max_size() noexcept;
2024-12-18 15:31:13 -08:00
// element access
2025-01-25 18:00:27 +02:00
constexpr reference operator[](size_type);
constexpr const_reference operator[](size_type) const;
constexpr reference at(size_type);
constexpr const_reference at(size_type) const;
constexpr reference front();
constexpr const_reference front() const;
constexpr reference back();
constexpr const_reference back() const;
constexpr T* data() noexcept;
constexpr const T* data() const noexcept;
T* c_array() noexcept; // deprecated
2024-12-18 15:31:13 -08:00
// modifiers
2025-01-25 18:00:27 +02:00
constexpr void swap(array<T, N>&);
constexpr void fill(const T&);
void assign(const T&); // deprecated
2024-12-18 15:31:13 -08:00
// public data members
T elems[N];
};
```
2025-01-25 18:00:27 +02:00
### Construct/Copy/Destroy
2024-12-18 15:31:13 -08:00
```
template<typename U> array& operator=(const array<U, N>& other);
```
[horizontal]
2025-01-25 18:00:27 +02:00
Effects: :: `std::copy(rhs.begin(), rhs.end(), begin())`.
2024-12-18 15:31:13 -08:00
---
2025-01-25 18:00:27 +02:00
### Iterator Support
2024-12-18 15:31:13 -08:00
```
2025-01-25 18:00:27 +02:00
constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
constexpr const_iterator cbegin() const noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `data()`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `data() + size()`.
2024-12-18 15:31:13 -08:00
---
2025-01-25 18:00:27 +02:00
### Reverse Iterator Support
2024-12-18 15:31:13 -08:00
```
2025-01-25 18:00:27 +02:00
reverse_iterator rbegin() noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `reverse_iterator(end())`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator crbegin() const noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `const_reverse_iterator(end())`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
reverse_iterator rend() noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `reverse_iterator(begin())`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crend() const noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `const_reverse_iterator(begin())`.
2024-12-18 15:31:13 -08:00
---
2025-01-25 18:00:27 +02:00
### Capacity
2024-12-18 15:31:13 -08:00
```
2025-01-25 18:00:27 +02:00
static constexpr size_type size() noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `N`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
static constexpr bool empty() noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `N == 0`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
static constexpr size_type max_size() noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `N`.
2024-12-18 15:31:13 -08:00
---
2025-01-25 18:00:27 +02:00
### Element Access
2024-12-18 15:31:13 -08:00
```
2025-01-25 18:00:27 +02:00
constexpr reference operator[](size_type i);
constexpr const_reference operator[](size_type i) const;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Requires: :: `i < N`.
Returns: :: `elems[i]`.
Throws: :: nothing.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
constexpr reference at(size_type i);
constexpr const_reference at(size_type i) const;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `elems[i]`.
Throws: :: `std::out_of_range` if `i >= N`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
constexpr reference front();
constexpr const_reference front() const;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Requires: :: `N > 0`.
Returns: :: `elems[0]`.
Throws: :: nothing.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
constexpr reference back();
constexpr const_reference back() const;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Requires: :: `N > 0`.
Returns: :: `elems[N-1]`.
Throws: :: nothing.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
constexpr T* data() noexcept;
constexpr const T* data() const noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `elems`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
T* c_array() noexcept; // deprecated
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `data()`.
Remarks: :: This function is deprecated. Use `data()` instead.
2024-12-18 15:31:13 -08:00
---
2025-01-25 18:00:27 +02:00
### Modifiers
2024-12-18 15:31:13 -08:00
```
2025-01-25 18:00:27 +02:00
constexpr void swap(array<T, N>& other);
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Effects: :: for each `i` in `[0..N)`, calls `swap(elems[i], other.elems[i])`.
Complexity: :: linear in `N`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
void fill(const T& value);
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Effects: :: for each `i` in `[0..N)`, performs `elems[i] = value;`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
void assign(const T& value); // deprecated
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Effects: :: `fill(value)`.
Remarks: :: An obsolete and deprecated spelling of `fill`. Use `fill` instead.
2024-12-18 15:31:13 -08:00
---
2025-01-25 18:00:27 +02:00
### Specialized Algorithms
2024-12-18 15:31:13 -08:00
```
2025-01-25 18:00:27 +02:00
template<typename T, std::size_t N>
constexpr void swap(array<T, N>& x, array<T, N>& y);
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Effects: :: `x.swap(y)`.
2024-12-18 15:31:13 -08:00
---
2025-01-25 18:00:27 +02:00
### Comparisons
2024-12-18 15:31:13 -08:00
```
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator==(const array<T, N>& x, const array<T, N>& y);
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `std::equal(x.begin(), x.end(), y.begin())`.
2024-12-18 15:31:13 -08:00
---
```
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator!=(const array<T, N>& x, const array<T, N>& y);
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `!(x == y)`.
2024-12-18 15:31:13 -08:00
---
```
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator<(const array<T, N>& x, const array<T, N>& y);
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end())`.
2024-12-18 15:31:13 -08:00
---
```
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator>(const array<T, N>& x, const array<T, N>& y);
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `y < x`.
2024-12-18 15:31:13 -08:00
---
```
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator<=(const array<T, N>& x, const array<T, N>& y);
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `!(y < x)`.
2024-12-18 15:31:13 -08:00
---
```
template<typename T, std::size_t N>
2025-01-25 18:00:27 +02:00
constexpr bool operator>=(const array<T, N>& x, const array<T, N>& y);
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Returns: :: `!(x < y)`.
2024-12-18 15:31:13 -08:00
---
2025-01-25 18:00:27 +02:00
### Specializations
2024-12-18 15:31:13 -08:00
```
2025-01-25 18:00:27 +02:00
template<std::size_t Idx, typename T, std::size_t N>
constexpr T& get(array<T, N>& arr) noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Mandates: :: `Idx < N`.
Returns: :: `arr[Idx]`.
2024-12-18 15:31:13 -08:00
---
```
2025-01-25 18:00:27 +02:00
template<std::size_t Idx, typename T, std::size_t N>
constexpr const T& get(const array<T, N>& arr) noexcept;
2024-12-18 15:31:13 -08:00
```
[horizontal]
2025-01-25 18:00:27 +02:00
Mandates: :: `Idx < N`.
Returns: :: `arr[Idx]`.
2024-12-18 15:31:13 -08:00
---