Section

std::basic_string_view

The class template std::basic_string_view is a lightweight non-owning view of a constant contiguous character sequence.

basic_string_view is for observing text, not owning it. Copying a string view copies only the pointer/length pair, so it is cheap to pass by value, but the referenced characters must outlive the view.

# Declarations

template< class CharT, class Traits = std::char_traits<CharT> >
class basic_string_view;

(since C++17)

# Template parameters

ParameterDescription
CharTCharacter type
TraitsCharacter traits type. Traits::char_type must be the same type as CharT.

# Semantic model

  • A string view does not allocate and does not own storage.
  • The viewed character range is constant; the view can be reseated or shortened, but it does not modify the underlying characters.
  • Size is tracked explicitly, so a string view may refer to substrings and character ranges that are not null-terminated.
  • The view is contiguous and tuple-free: element access, iteration, comparison, and search all operate directly on the referenced range.

# Example

#include <iostream>
#include <string_view>
 
int main()
{
    constexpr std::string_view unicode[]{"▀▄─", "▄▀─", "▀─▄", "▄─▀"};
 
    for (int y{}, p{}; y != 6; ++y, p = ((p + 1) % 4))
    {
        for (int x{}; x != 16; ++x)
            std::cout << unicode[p];
        std::cout << '\n';
    }
}

This is the central usage pattern: pass a cheap read-only view into existing character storage without copying the string data.

# Member types

Member typeDefinition
traits_typeTraits
value_typeCharT
pointerCharT*
const_pointerconst CharT*
referenceCharT&
const_referenceconst CharT&
const_iteratorimplementation-defined constant contiguous iterator over the character sequence
iteratorconst_iterator
const_reverse_iteratorstd::reverse_iterator<const_iterator>
reverse_iteratorconst_reverse_iterator
size_typestd::size_t
difference_typestd::ptrdiff_t

iterator and const_iterator are the same type because basic_string_view views constant characters.

# Reference map

AreaKey entries
Construction and lifetimebasic_string_view::basic_string_view, basic_string_view::operator=, swap
Iteratorsbegin, cbegin, end, cend, rbegin, crbegin, rend, crend
Element accessoperator[], at, front, back, data
Capacity and slicingsize, length, max_size, empty, remove_prefix, remove_suffix, substr
Comparison and searchcompare, starts_with, ends_with, contains, find, rfind, find_first_of, find_last_of, find_first_not_of, find_last_not_of
Constants and non-member functionsnpos, comparison operators, operator«, operator""sv
Related supportstd::hash specializations for string_view types, deduction guides

# Helper templates

  • ranges::enable_borrowed_range<std::basic_string_view<CharT, Traits>> = true makes basic_string_view a borrowed range in C++20.
  • ranges::enable_view<std::basic_string_view<CharT, Traits>> = true makes basic_string_view a view in C++20.

# Deduction guides

The class has deduction guides so construction from compatible character ranges can infer the CharT and Traits parameters.

# Notes

It is the programmer’s responsibility to ensure that the referenced characters outlive the view. A std::string_view can safely refer to a string literal, but it can easily dangle when bound to a temporary std::string.

Specializations of std::basic_string_view were already trivially copyable in practice before that guarantee became a formal requirement in C++23.

# Feature-test macros

MacroValueStandardMeaning
__cpp_lib_string_view201606LC++17std::string_view
__cpp_lib_string_view201803LC++20constexpr iterators
__cpp_lib_string_contains202011LC++23contains

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3203C++17only pointers, iterators, and references returned from member functions might be invalidatedall pointers, iterators, and references to elements may be invalidated

# See also