Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c86be24

Browse files
committed
Revert "Revert "Revert "Removed gsl::string_span, recommend using std::string_view, std::span<char>, or gsl::span<char> instead"""
This reverts commit 8241609.
1 parent 8241609 commit c86be24

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

CppCoreGuidelines.md

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ This `draw2()` passes the same amount of information to `draw()`, but makes the
19821982
##### Exception
19831983

19841984
Use `zstring` and `czstring` to represent C-style, zero-terminated strings.
1985-
But when doing so, use `std::string_view` or `span<char>` from the [GSL](#S-gsl) to prevent range errors.
1985+
But when doing so, use `std::string_view` or `string_span` from the [GSL](#S-gsl) to prevent range errors.
19861986

19871987
##### Enforcement
19881988

@@ -5620,7 +5620,7 @@ An initialization explicitly states that initialization, rather than assignment,
56205620

56215621
##### Example, better still
56225622

5623-
Instead of those `const char*`s we could use C++17 `std::string_view` or `gsl::span<char>`
5623+
Instead of those `const char*`s we could use `gsl::string_span` or (in C++17) `std::string_view`
56245624
as [a more general way to present arguments to a function](#Rstr-view):
56255625

56265626
class D { // Good
@@ -9051,7 +9051,7 @@ Whenever you deal with a resource that needs paired acquire/release function cal
90519051

90529052
Consider:
90539053

9054-
void send(X* x, string_view destination)
9054+
void send(X* x, cstring_span destination)
90559055
{
90569056
auto port = open_port(destination);
90579057
my_mutex.lock();
@@ -9070,7 +9070,7 @@ Further, if any of the code marked `...` throws an exception, then `x` is leaked
90709070

90719071
Consider:
90729072

9073-
void send(unique_ptr<X> x, string_view destination) // x owns the X
9073+
void send(unique_ptr<X> x, cstring_span destination) // x owns the X
90749074
{
90759075
Port port{destination}; // port owns the PortHandle
90769076
lock_guard<mutex> guard{my_mutex}; // guard owns the lock
@@ -9086,7 +9086,7 @@ What is `Port`? A handy wrapper that encapsulates the resource:
90869086
class Port {
90879087
PortHandle port;
90889088
public:
9089-
Port(string_view destination) : port{open_port(destination)} { }
9089+
Port(cstring_span destination) : port{open_port(destination)} { }
90909090
~Port() { close_port(port); }
90919091
operator PortHandle() { return port; }
90929092

@@ -19653,7 +19653,7 @@ Instead, define proper default initialization, copy, and comparison functions
1965319653

1965419654
Text manipulation is a huge topic.
1965519655
`std::string` doesn't cover all of it.
19656-
This section primarily tries to clarify `std::string`'s relation to `char*`, `zstring`, `string_view`, and `gsl::span<char>`.
19656+
This section primarily tries to clarify `std::string`'s relation to `char*`, `zstring`, `string_view`, and `gsl::string_span`.
1965719657
The important issue of non-ASCII character sets and encodings (e.g., `wchar_t`, Unicode, and UTF-8) will be covered elsewhere.
1965819658

1965919659
**See also**: [regular expressions](#SS-regex)
@@ -19664,13 +19664,13 @@ We don't consider ???
1966419664
String summary:
1966519665

1966619666
* [SL.str.1: Use `std::string` to own character sequences](#Rstr-string)
19667-
* [SL.str.2: Use `std::string_view` or `gsl::span<char>` to refer to character sequences](#Rstr-view)
19667+
* [SL.str.2: Use `std::string_view` or `gsl::string_span` to refer to character sequences](#Rstr-view)
1966819668
* [SL.str.3: Use `zstring` or `czstring` to refer to a C-style, zero-terminated, sequence of characters](#Rstr-zstring)
1966919669
* [SL.str.4: Use `char*` to refer to a single character](#Rstr-char*)
1967019670
* [SL.str.5: Use `std::byte` to refer to byte values that do not necessarily represent characters](#Rstr-byte)
1967119671

1967219672
* [SL.str.10: Use `std::string` when you need to perform locale-sensitive string operations](#Rstr-locale)
19673-
* [SL.str.11: Use `gsl::span<char>` rather than `std::string_view` when you need to mutate a string](#Rstr-span)
19673+
* [SL.str.11: Use `gsl::string_span` rather than `std::string_view` when you need to mutate a string](#Rstr-span)
1967419674
* [SL.str.12: Use the `s` suffix for string literals meant to be standard-library `string`s](#Rstr-s)
1967519675

1967619676
**See also**:
@@ -19708,6 +19708,16 @@ In C++17, we might use `string_view` as the argument, rather than `const string&
1970819708
return res;
1970919709
}
1971019710

19711+
The `gsl::string_span` is a current alternative offering most of the benefits of `std::string_view` for simple examples:
19712+
19713+
vector<string> read_until(string_span terminator)
19714+
{
19715+
vector<string> res;
19716+
for (string s; cin >> s && s != terminator; ) // read a word
19717+
res.push_back(s);
19718+
return res;
19719+
}
19720+
1971119721
##### Example, bad
1971219722

1971319723
Don't use C-style strings for operations that require non-trivial memory management
@@ -19738,18 +19748,18 @@ Do not assume that `string` is slower than lower-level techniques without measur
1973819748

1973919749
???
1974019750

19741-
### <a name="Rstr-view"></a>SL.str.2: Use `std::string_view` or `gsl::span<char>` to refer to character sequences
19751+
### <a name="Rstr-view"></a>SL.str.2: Use `std::string_view` or `gsl::string_span` to refer to character sequences
1974219752

1974319753
##### Reason
1974419754

19745-
`std::string_view` or `gsl::span<char>` provides simple and (potentially) safe access to character sequences independently of how
19755+
`std::string_view` or `gsl::string_span` provides simple and (potentially) safe access to character sequences independently of how
1974619756
those sequences are allocated and stored.
1974719757

1974819758
##### Example
1974919759

19750-
vector<string> read_until(string_view terminator);
19760+
vector<string> read_until(string_span terminator);
1975119761

19752-
void user(zstring p, const string& s, string_view ss)
19762+
void user(zstring p, const string& s, string_span ss)
1975319763
{
1975419764
auto v1 = read_until(p);
1975519765
auto v2 = read_until(s);
@@ -19829,7 +19839,7 @@ The array `arr` is not a C-style string because it is not zero-terminated.
1982919839

1983019840
##### Alternative
1983119841

19832-
See [`zstring`](#Rstr-zstring), [`string`](#Rstr-string), and [`string_view`](#Rstr-view).
19842+
See [`zstring`](#Rstr-zstring), [`string`](#Rstr-string), and [`string_span`](#Rstr-view).
1983319843

1983419844
##### Enforcement
1983519845

@@ -19873,7 +19883,7 @@ C++17
1987319883

1987419884
???
1987519885

19876-
### <a name="Rstr-span"></a>SL.str.11: Use `gsl::span<char>` rather than `std::string_view` when you need to mutate a string
19886+
### <a name="Rstr-span"></a>SL.str.11: Use `gsl::string_span` rather than `std::string_view` when you need to mutate a string
1987719887

1987819888
##### Reason
1987919889

@@ -20904,6 +20914,8 @@ If something is not supposed to be `nullptr`, say so:
2090420914

2090520915
* `span<T>` // `[p:p+n)`, constructor from `{p, q}` and `{p, n}`; `T` is the pointer type
2090620916
* `span_p<T>` // `{p, predicate}` `[p:q)` where `q` is the first element for which `predicate(*p)` is true
20917+
* `string_span` // `span<char>`
20918+
* `cstring_span` // `span<const char>`
2090720919

2090820920
A `span<T>` refers to zero or more mutable `T`s unless `T` is a `const` type.
2090920921

@@ -21699,9 +21711,9 @@ Because we want to use them immediately, and because they are temporary in that
2169921711

2170021712
No. The GSL exists only to supply a few types and aliases that are not currently in the standard library. If the committee decides on standardized versions (of these or other types that fill the same need) then they can be removed from the GSL.
2170121713

21702-
### <a name="Faq-gsl-string-view"></a>FAQ.55: If you're using the standard types where available, why is the GSL `span<char>` different from the `string_view` in the Library Fundamentals 1 Technical Specification and C++17 Working Paper? Why not just use the committee-approved `string_view`?
21714+
### <a name="Faq-gsl-string-view"></a>FAQ.55: If you're using the standard types where available, why is the GSL `string_span` different from the `string_view` in the Library Fundamentals 1 Technical Specification and C++17 Working Paper? Why not just use the committee-approved `string_view`?
2170321715

21704-
The consensus on the taxonomy of views for the C++ Standard Library was that "view" means "read-only", and "span" means "read/write". If you only need a read-only view of characters that does not need guaranteed bounds-checking and you have C++17, use C++17 `std::string_view`. Otherwise, if you need a read-write view that does not need guaranteed bounds-checking and you have C++20, use C++20 `std::span<char>`. Otherwise, use `gsl::span<char>`.
21716+
The consensus on the taxonomy of views for the C++ Standard Library was that "view" means "read-only", and "span" means "read/write". The read-only `string_view` was the first such component to complete the standardization process, while `span` and `string_span` are currently being considered for standardization.
2170521717

2170621718
### <a name="Faq-gsl-owner"></a>FAQ.56: Is `owner` the same as the proposed `observer_ptr`?
2170721719

@@ -22220,7 +22232,7 @@ Better:
2222022232
A checker must consider all "naked pointers" suspicious.
2222122233
A checker probably must rely on a human-provided list of resources.
2222222234
For starters, we know about the standard-library containers, `string`, and smart pointers.
22223-
The use of `span` and `string_view` should help a lot (they are not resource handles).
22235+
The use of `span` and `string_span` should help a lot (they are not resource handles).
2222422236

2222522237
### <a name="Cr-raw"></a>Discussion: A "raw" pointer or reference is never a resource handle
2222622238

0 commit comments

Comments
 (0)