You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/gsl-intro.md
+17-8
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
4
4
by Herb Sutter
5
5
6
-
updated 2017-04-17
6
+
updated 2017-05-24
7
7
8
8
9
9
## Overview: "Is this document a tutorial or a FAQ?"
@@ -21,10 +21,16 @@ First look at the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuideli
21
21
22
22
You can try out the examples in this document on all major compilers and platforms using [this GSL reference implementation](https://github.com/microsoft/gsl).
23
23
24
-
# gsl::span: "What is gsl::span for?"
24
+
25
+
# gsl::span: "What is gsl::span, and what is it for?"
25
26
26
27
`gsl::span` is a replacement for `(pointer, length)` pairs to refer to a sequence of contiguous objects. It can be thought of as a pointer to an array, but that knows its bounds.
27
28
29
+
For example, a `span<int,7>` refers to a sequence of seven contiguous `int`s.
30
+
31
+
A `span` does not own the elements it points to. It is not a container like an `array` or a `vector`, it is a view into the contents of such a container.
32
+
33
+
28
34
## span parameters: "How should I choose between span and traditional (ptr, length) parameters?"
29
35
30
36
In new code, prefer the bounds-checkable `span<T>` instead of separate pointer and length parameters. In older code, adopt `span` where reasonable as you maintain the code.
Note that this is bounds-safe with zero overhead, because there is no range check needed -- the range-`for` loop is known by construction to not exceed the range's bounds.
126
+
127
+
128
+
## Element access: "How do I access a single element in a span?"
129
+
130
+
Use `myspan[offset]` to subscript, or equivalently use `iter + offset` wheren `iter` is a `span<T>::iterator`. Both are range-checked.
131
+
132
+
124
133
125
134
## Sub-spans: "What if I need a subrange of a span?"
126
135
@@ -254,7 +263,7 @@ void serialize(span<const byte>); // can't forget const, the first test call sit
0 commit comments