@@ -15,20 +15,22 @@ It can be thought of like a...
15
15
* ` std::array ` except the size is specified at runtime.
16
16
* ` std::vector ` except it can neither shrink nor grow.
17
17
18
- ### Container Named Requirements
19
- In order to allow element access using iterators and to align with various container
20
- idioms for ` std:: ` algorithms, ` gsl::dyn_array ` should satisfy the following named
21
- requirements:
22
-
23
- * Container ([ link] ( https://en.cppreference.com/w/cpp/named_req/Container ) )
24
- * ReversibleContainer ([ link] ( https://en.cppreference.com/w/cpp/named_req/ReversibleContainer ) )
25
- * ContiguousContainer ([ link] ( https://en.cppreference.com/w/cpp/named_req/ContiguousContainer ) )
26
- * SequenceContainer ([ link] ( https://en.cppreference.com/w/cpp/named_req/SequenceContainer ) )
27
- * AllocatorAwareContainer ([ link] ( https://en.cppreference.com/w/cpp/named_req/AllocatorAwareContainer ) )
18
+ By design, ` gsl::dyn_array ` is not a ` Container ` as defined by the C++ Named
19
+ Requirements because we want to avoid the invalidation of iterators or references to
20
+ ` gsl::dyn_array ` objects.
28
21
29
22
### Construction
30
- In addition to the constructors required by the named requirements (default, copy, and
31
- move), ` gsl::dyn_array ` will support the following constructors:
23
+ ` gsl::dyn_array ` s can be constructed in the following ways:
24
+
25
+ * Default construct a ` dyn_array ` with no elements:
26
+ ``` c++
27
+ constexpr dyn_array ();
28
+ ```
29
+
30
+ * Move construct a ` dyn_array ` from ` other ` :
31
+ ``` c++
32
+ constexpr dyn_array (dyn_array&& other) noexcept;
33
+ ```
32
34
33
35
* Construct a `dyn_array` with `n` default constructed elements:
34
36
```c++
@@ -43,20 +45,24 @@ constexpr dyn_array(size_t n, const T& arg, const Allocator & alloc = Allocator(
43
45
* Construct a `dyn_array` with elements from the range `[first, last)`:
44
46
```c++
45
47
template <typename InputIt>
46
- requires (std::input_iterator<InputIt >)
48
+ #ifdef __cpp_lib_concepts
49
+ requires(std::input_iterator<InputIt>)
50
+ #endif /* __cpp_lib_concepts */
47
51
constexpr dyn_array(InputIt first, InputIt last, const Allocator & alloc = Allocator());
48
52
```
49
53
50
- * Construct a `dyn_array` with elements from the initializer list :
54
+ * Construct a ` dyn_array ` from a range :
51
55
``` c++
52
- constexpr dyn_array(std::initializer_list<T>, const Allocator & alloc = Allocator());
56
+ #ifdef __cpp_lib_containers_range
57
+ template <typename R >
58
+ requires(std::ranges::range<R >)
59
+ constexpr dyn_array(std::from_range_t, R&& r, const Allocator & alloc = Allocator());
60
+ #endif /* __cpp_lib_containers_range */
53
61
```
54
62
55
- * Construct a ` dyn_array ` with elements from the range ` R ` :
63
+ * Construct a ` dyn_array ` with elements from the initializer list :
56
64
``` c++
57
- template <typename R>
58
- requires (std::input_range<R >)
59
- constexpr dyn_array(R&&, const Allocator & alloc = Allocator());
65
+ constexpr dyn_array (std::initializer_list<T >, const Allocator & alloc = Allocator());
60
66
```
61
67
62
68
### Operations
@@ -66,7 +72,7 @@ support the following operations:
66
72
* Access the specified element **_with bounds checking_**:
67
73
```c++
68
74
constexpr T& operator[](size_t);
69
- constexpr const T& operator[](size_t) const
75
+ constexpr const T& operator[](size_t) const;
70
76
```
71
77
72
78
* Access the underlying array:
@@ -80,10 +86,17 @@ constexpr const T* data() const noexcept;
80
86
constexpr size_t size () const noexcept ;
81
87
```
82
88
83
- #### Note: Why no push_back (and friends)?
89
+ ### FAQ
90
+
91
+ #### Why no push_back (and friends)?
84
92
` gsl::dyn_array ` is intended to be a fixed-size array and all objects should be
85
- constructed at creation. Moreover, the memory overhead of storing another member
86
- variable to track where to push the next item is not desired.
93
+ constructed at creation.
94
+
95
+ #### Why does ` gsl::dyn_array ` not conform to the ` Container ` Named Requirements?
96
+ ` gsl::dyn_array ` is intended to be a safer replacement for raw pointers and sizes. We
97
+ don't want users to accidentally use it in a way that would be unsafe. For example,
98
+ ` gsl::dyn_array ` does not have copy or move assignment operators. This is because it
99
+ would be possible to invalidate existing iterators and references.
87
100
88
101
### Bounds Checking Semantics
89
102
If an out-of-bounds access (read or write) is attempted, ` gsl::dyn_array ` should follow
0 commit comments