std::basic_string_view
<string_view>
class CharT,
class Traits = std::char_traits <CharT>
The class template basic_string_view
describes an object that can refer to a constant contiguous sequence of CharT
with the first element of the sequence at position zero.
For a basic_string_view
str, pointers, iterators, and references to elements of str are invalidated when an operation invalidates a pointer in the range [
str.data(),
str.data() + str.size())
.
Every specialization of std::basic_string_view
is a TriviallyCopyable type.
Several typedefs for common character types are provided:
<string_view>
Contents
[edit] Template parameters
Traits::char_type
must name the same type as CharT
or the program is ill-formed.
[edit] Nested types
traits_type
Traits
value_type
CharT
pointer
CharT*
const_pointer
const CharT*
reference
CharT&
const_reference
const CharT&
const_iterator
implementation-defined constant LegacyRandomAccessIterator,whose value_type
is CharT
iterator
const_iterator
const_reverse_iterator
reverse_iterator
const_reverse_iterator
size_type
difference_type
Note: iterator
and const_iterator
are the same type because string views are views into constant character sequences.
All requirements on the iterator types of a Container applies to the iterator
and const_iterator
types of basic_string_view
as well.
[edit] Data members
const_pointer
data_
a pointer to the underlying sequence(exposition-only member object*)
size_type
size_
the number of characters(exposition-only member object*)
[edit] Member functions
Constructors and assignment
Iterators
Element access
Capacity
Modifiers
Operations
(public member function) [edit]
Constants
(public static member constant) [edit]
[edit] Non-member functions
(function template) [edit]
Input/output
[edit] Literals
std::literals::string_view_literals
[edit] Helper classes
(class template specialization) [edit]
[edit] Helper templates
inline constexpr bool
This specialization of ranges::enable_borrowed_range makes basic_string_view
satisfy borrowed_range
.
inline constexpr bool
This specialization of ranges::enable_view makes basic_string_view
satisfy view
.
Deduction guides
(since C++20)[edit] Notes
It is the programmer's responsibility to ensure that std::string_view
does not outlive the pointed-to character array:
std::string_view good{"a string literal"}; // "Good" case: `good` points to a static array. // String literals reside in persistent data storage. std::string_view bad{"a temporary string"s}; // "Bad" case: `bad` holds a dangling pointer since the std::string temporary, // created by std::operator""s, will be destroyed at the end of the statement.
Specializations of std::basic_string_view
are already trivially copyable types in all existing implementations, even before the formal requirement introduced in C++23.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_string_view |
201606L |
(C++17) | std::string_view
|
201803L |
(C++20) | ConstexprIterator | |
__cpp_lib_string_contains |
202011L |
(C++23) | contains
|
[edit] Example
Output:
▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─ ▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─ ▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄ ▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀ ▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─ ▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─ ▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄ ▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3203 | C++17 | only pointers, iterators, and references returned from the member functions of basic_string_view might be invalidated
|
all pointers, iterators, and references to elements of basic_string_view may be invalidated |
[edit] See also
(function template) [edit]