Skip to content

Commit e6fbb18

Browse files
authored
Merge pull request #2 from TylerMSFT/StephanTLavavej-patch-2
Update span-class.md
2 parents 1b7df3e + 21e7d8f commit e6fbb18

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

docs/standard-library/span-class.md

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ helpviewer_keywords: ["std::span [C++]", "std::span [C++], const_iterator", "std
88
---
99
# span Class (C++ Standard Library)
1010

11-
Provides a lightweight view over a contiguous sequence of objects. A span provides a safe way to iterate over, and index into, objects that are arranged back-to-back in memory.
11+
Provides a lightweight view over a contiguous sequence of objects. A span provides a safe way to iterate over, and index into, objects that are arranged back-to-back in memory (for example, objects stored in a built-in array, `std::array`, or `std::vector`).
1212

13-
If you typically access a sequence of back-to-back objects using a pointer indexed by the size of the objects, span is a safer, lightweight alternative.
13+
If you typically access a sequence of back-to-back objects using a pointer and an index, span is a safer, lightweight alternative.
1414

15-
A span's size is determined at compile time by specifying it as part of the template declaration, or at runtime by specifying `dynamic-size`.
15+
A span's size is determined at compile time by specifying it as a template argument, or at runtime by specifying `dynamic_extent`.
1616

1717
## Syntax
1818

@@ -34,8 +34,8 @@ class span;
3434
3535
| **Type Definitions** | **Description** |
3636
|-|-|
37-
| [const_pointer](#pointer) | The type of a const pointer to a span element. |
38-
| [const_reference](#reference) | The type of a const reference to an element. |
37+
| [const_pointer](#pointer) | The type of a pointer to a `const` element. |
38+
| [const_reference](#reference) | The type of a reference to a `const` element. |
3939
| [difference_type](#difference_type) | The type of a signed distance between two elements. |
4040
| [element_type](#element_type) | The type of a span element. |
4141
| [iterator](#iterator) | The type of an iterator for a span. |
@@ -66,20 +66,22 @@ class span;
6666
| [subspan](#sub_view) | Gets a subspan from anywhere in the span.|
6767
| **Operators** | **Description** |
6868
|[span::operator=](#op_eq)| Replaces the span.|
69-
|[span::operator\[\]](#op_at)| Access an element at a specified position.|
69+
|[span::operator\[\]](#op_at)| Access an element at a specified position. (FIXME: MENTIONED TWICE?) |
7070
7171
## Remarks
7272
7373
All `span` member functions have constant time complexity.
7474
75-
Unlike array or vector, a span doesn't "own the elements inside it. A span doesn't free any storage for the items inside it because it doesn't own the storage for those objects.
75+
Unlike `array` or `vector`, a span doesn't "own" the elements inside it. A span doesn't free any storage for the items inside it because it doesn't own the storage for those objects.
7676
7777
## Requirements
7878
7979
**Header:** \<span>
8080
8181
**Namespace:** std
8282
83+
**Compiler Option:** /std:c++latest
84+
8385
## <a name="back"></a> `span::back`
8486
8587
Returns the last element in the span.
@@ -97,11 +99,12 @@ A reference to the last element in the span.
9799
```cpp
98100
#include <span>
99101
#include <iostream>
102+
using namespace std; // FIXME: Lack of qualification occurs in many examples below; need using or std::
100103

101-
void main()
104+
int main() // FIXME: void main() occurs in many examples below
102105
{
103106
int a[] = { 0,1,2 };
104-
span <int> mySpan(a);
107+
span<int> mySpan(a); // FIXME, STYLE: span <int> with an unnecessary space occurs in many examples below
105108

106109
cout << mySpan.back();
107110
}
@@ -116,7 +119,7 @@ void main()
116119
Returns an iterator pointing at the first element in the span.
117120

118121
```cpp
119-
constexpr iterator begin() const noexcept
122+
constexpr iterator begin() const noexcept;
120123
```
121124

122125
### Return value
@@ -153,7 +156,7 @@ constexpr pointer data() const noexcept;
153156

154157
### Return Value
155158

156-
A pointer to the first item stored in the span. If the span is empty, returns `nullptr`.
159+
A pointer to the first item stored in the span.
157160

158161
### Example
159162

@@ -174,7 +177,6 @@ void main()
174177
0
175178
```
176179

177-
[difference_type](#difference_type)
178180
## <a name="difference_type"></a> `span::difference_type`
179181

180182
This type represents the number of elements between two elements in a span.
@@ -194,13 +196,13 @@ void main()
194196
int a[] = { 0,1,2 };
195197
span <int> mySpan(a);
196198

197-
pan<int>::difference_type distance = mySpan[2] - mySpan[0];
199+
span<int>::difference_type distance = mySpan.end() - mySpan.begin();
198200
cout << distance << std::endl;
199201
}
200202
```
201203

202204
```Output
203-
2
205+
3
204206
```
205207

206208
## <a name="element_type"></a> `span::element_type`
@@ -241,7 +243,7 @@ void main()
241243
Whether the span contains elements.
242244

243245
```cpp
244-
constexpr bool empty() const noexcept
246+
constexpr bool empty() const noexcept;
245247
```
246248

247249
### Return Value
@@ -267,7 +269,7 @@ void main()
267269
Returns an iterator to the end of the span.
268270

269271
```cpp
270-
constexpr iterator end() const;
272+
constexpr iterator end() const noexcept;
271273
```
272274

273275
### Return Value
@@ -295,7 +297,7 @@ for (auto it = s1.begin(); it != s1.end(); ++it)
295297
Get a subspan, taken from the front of this span.
296298

297299
```cpp
298-
constexpr auto first(const size_type count) const noexcept;
300+
constexpr auto first(size_type count) const noexcept;
299301
template <size_t count> constexpr auto first() const noexcept;
300302
```
301303
@@ -368,7 +370,7 @@ void main()
368370
int a[] = { 0,1,2 };
369371
span <int> mySpan(a);
370372

371-
auto i = mySpan.first();
373+
auto i = mySpan.front();
372374
cout << i;
373375
}
374376
```
@@ -382,7 +384,7 @@ void main()
382384
The type of an iterator over span elements.
383385

384386
```cpp
385-
using iterator = _Span_iterator<T>;
387+
using iterator = implementation-defined-iterator-type;
386388
```
387389

388390
### Remarks
@@ -469,7 +471,7 @@ mySpan.last<2>: 12
469471
Access an element in the span at a specified position.
470472

471473
```cpp
472-
constexpr reference operator[](const size_type offset)
474+
constexpr reference operator[](size_type offset) const;
473475
```
474476

475477
### Parameters
@@ -491,7 +493,6 @@ void main()
491493
{
492494
int a[] = { 0,1,2 };
493495
span <int> mySpan(a);
494-
auto i = mySpan.begin();
495496
cout << mySpan[1];
496497
}
497498
```
@@ -531,9 +532,9 @@ void main()
531532
{
532533
int a[] = { 0,1,2 };
533534
span <int> mySpan(a);
534-
span <int> mySpan2 = mySpan;
535-
for (auto it : mySpan2)
536-
{
535+
span <int> mySpan2 = mySpan; // FIXME: This is a copy-initialization, not a copy assignment
536+
for (auto it : mySpan2) // FIXME: Range-for iterates over elements, not iterators. This will compile and work,
537+
{ // FIXME: but "it" is misleadingly named (and copied). Should be "auto& i : mySpan2" like other examples.
537538
cout << it;
538539
}
539540
}
@@ -653,7 +654,7 @@ void main()
653654
Get a random-access iterator to the sentinel just beyond the end of the reversed span.
654655

655656
```cpp
656-
constexpr reverse_iterator rend() const;
657+
constexpr reverse_iterator rend() const noexcept;
657658
```
658659

659660
### Return Value
@@ -774,7 +775,7 @@ void main()
774775

775776
## <a name="size_type"></a> `span::size_type`
776777

777-
The size of the type of an element in the span.
778+
An unsigned type, suitable for storing the number of elements in a span.
778779

779780
```cpp
780781
using size_type = size_t;
@@ -791,28 +792,29 @@ void main()
791792
int a[] = { 0,1,2 };
792793
span <int> mySpan(a);
793794

794-
span<int>::size_type szType = sizeof(mySpan[0]);
795+
span<int>::size_type szType = mySpan.size();
795796
cout << szType;
796797
}
797798
```
798799

799800
```Output
800-
4
801+
3
801802
```
802803

803804
## <a name="span"></a> `span::span`
804805

805806
`span` constructors.
806807

807808
```cpp
809+
// FIXME: dynamicExtent should be dynamic_extent below, repeatedly
808810
constexpr span() noexcept
809811
requires (Extent == 0 || Extent == dynamicExtent) = default;
810812

811-
template <It>
813+
template <class It>
812814
constexpr explicit(Extent != dynamicExtent)
813815
span(It first, size_type count) noexcept
814816

815-
template <It, End>
817+
template <class It, class End>
816818
constexpr explicit(Extent != dynamicExtent)
817819
span(It first, End last) noexcept(noexcept(last - first))
818820

@@ -874,7 +876,7 @@ A span doesn't free storage for items in the span because it doesn't own the sto
874876
|---------|---------|
875877
|`span()` | Constructs an empty span. Only considered during overload resolution when the template parameter `Extent` is `0` or `dynamic_extent`.|
876878
|`span(It first, size_type count)` | Constructs a span from the first `count` elements from iterator `first`. Only considered during overload resolution when template parameter `Extent` isn't `dynamic_extent`. |
877-
|`span(It first, End last)` | Constructs a span from the elements in iterator `first` until the sentinel `end` is reached. Only considered during overload resolution when template parameter `Extent` isn't `dynamic_extent`. `It` must be a `contiguous_iterator`. |
879+
|`span(It first, End last)` | Constructs a span from the elements in iterator `first` until the sentinel `last` is reached. Only considered during overload resolution when template parameter `Extent` isn't `dynamic_extent`. `It` must be a `contiguous_iterator`. |
878880
|`span(array<T, N>& arr) noexcept;`<br /><br />`span(const array<T, N>& arr) noexcept;`<br /><br />`span(type_identity_t<element_type> (&arr)[N]) noexcept;` | Constructs a span from `N` elements of the specified array. Only considered during overload resolution when template parameter `Extent` is `dynamic_extent` or equals `N`. |
879881
|`span(R&& r)` | Constructs a span from a range. Only participates in overload resolution if template parameter `Extent` isn't `dynamic_extent`.|
880882
|`span(const span& other)` | The compiler-generated copy constructor. A shallow copy of the data pointer is safe because the span doesn't allocate the memory to hold the elements. |
@@ -910,21 +912,18 @@ Gets a subspan from this span.
910912

911913
```cpp
912914
constexpr span<element_type, dynamic_extent>
913-
subspan(size_t offset, size_t count = dynamic_extent) const noexcept;
914-
915-
constexpr span<element_type, dynamic_extent>
916-
subspan(const size_type offset, const size_type count = dynamic_extent) const noexcept;
915+
subspan(size_type offset, size_type count = dynamic_extent) const noexcept;
917916

918917
template <size_t offset, size_t count = dynamic_extent>
919918
constexpr span<element_type,
920-
count != dynamic_extent ? count : (Extent != dynamic_extent ? Extent - _Offset : )>
919+
count != dynamic_extent ? count : (Extent != dynamic_extent ? Extent - _Offset : FIXME)>
921920
subspan() const noexcept
922921
```
923922
924923
### Parameters
925924
926925
*count*\
927-
The number of elements from the end this span to put in the subspan. If span is `dynamic_extent` (the default value), then gets the elements from `offset` to the end of this span.
926+
The number of elements to put in the subspan. If `count` is `dynamic_extent` (the default value), then gets the elements from `offset` to the end of this span.
928927
929928
*offset*\
930929
The location in this span to start the subspan.
@@ -958,7 +957,7 @@ void main()
958957
{
959958
cout << i;
960959
}
961-
cout << "\nmySpan.subspan<1>: ";
960+
cout << "\nmySpan.subspan<1>: "; // FIXME: Doesn't match actual call
962961
for (auto& i : mySpan.subspan(1))
963962
{
964963
cout << i;
@@ -1025,11 +1024,11 @@ span(It, End) -> span<remove_reference_t<iter_reference_t<It>>>;
10251024
// Allows the element type to be deduced from a range.
10261025
// The range must be contiguous
10271026

1028-
template <_RANGES contiguous_range Rng>
1029-
span(Rng &&) -> span<remove_reference_t<_RANGES range_reference_t<Rng>>>;
1027+
template <ranges::contiguous_range Rng>
1028+
span(Rng &&) -> span<remove_reference_t<ranges::range_reference_t<Rng>>>;
10301029
```
10311030
10321031
## See also
10331032
10341033
[\<span>](../standard-library/span.md)
1035-
[How to use class template argument deduction](https://devblogs.microsoft.com/cppblog/how-to-use-class-template-argument-deduction/)
1034+
[How to use class template argument deduction](https://devblogs.microsoft.com/cppblog/how-to-use-class-template-argument-deduction/)

0 commit comments

Comments
 (0)