std::basic_string_view(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌈 std::basic_string_view 🌈

okay bestie, this is the std::basic_string_view(3) page from the C++ Standard Library section β€” yes, the header really spells it C++ Standard Libary, and honestly 😭. This is part 1, so buckle up; the facts are all here, the vibe is just, like, way more fun now πŸ’….


πŸ’– NAME πŸ’–

std::basic_string_view β€” std::basic_string_view (yes bestie, the NAME section is literally the same string twice; we love a minimal icon πŸ‘‘).

✨ Synopsis ✨

okay so check it: this is defined in header <string_view>, and the template situation is:

Defined in header <string_view>
template<

    class CharT,                            (since C++17)
    class Traits = std::char_traits<CharT>

> class basic_string_view;

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. Slay, but make it read-only ✨.

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()). That range is literally the whole vibe check, bestie.

Every specialization of std::basic_string_view is a TriviallyCopyable (since C++23) type. A typical implementation holds only two members: a pointer to constant CharT and a size. Lowkey elegant, no cap πŸŽ€.

Several typedefs for common character types are provided:

Defined in header <string_view>

  • std::string_view (C++17) β€” std::basic_string_view<char>
  • std::wstring_view (C++17) β€” std::basic_string_view<wchar_t>
  • std::u8string_view (C++20) β€” std::basic_string_view<char8_t>
  • std::u16string_view (C++17) β€” std::basic_string_view<char16_t>
  • std::u32string_view (C++17) β€” std::basic_string_view<char32_t>

πŸŽ€ Template parameters πŸŽ€

  • CharT β€” character type.
  • Traits β€” CharTraits class specifying the operations on the character type. Like for std::basic_string, Traits::char_type must name the same type as CharT or the program is ill-formed.

πŸ¦‹ 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, and LegacyContiguousIterator (until C++20); ConstexprIterator, and contiguous_iterator (since C++20), whose value_type is CharT
  • iterator β€” const_iterator
  • const_reverse_iterator β€” std::reverse_iterator<const_iterator>
  • reverse_iterator β€” const_reverse_iterator
  • size_type β€” std::size_t
  • difference_type β€” std::ptrdiff_t
Note: iterator and const_iterator are the same type because string views are views into constant character sequences. Iconic behavior, honestly 🫢.

All requirements on the iterator types of a Container applies to the iterator and const_iterator types of basic_string_view as well. fr fr.

πŸ’… Member functions πŸ’…

Here's the whole menu, grouped exactly like the page groups it, bestie.

Constructors and assignment

  • constructor β€” constructs a basic_string_view (public member function)
  • operator= β€” assigns a view (public member function)

Iterators

  • begin β€” returns an iterator to the beginning (public member function)
  • cbegin β€” (public member function)
  • end β€” returns an iterator to the end (public member function)
  • cend β€” (public member function)
  • rbegin β€” returns a reverse iterator to the beginning (public member function)
  • crbegin β€” (public member function)
  • rend β€” returns a reverse iterator to the end (public member function)
  • crend β€” (public member function)

Element access

  • operator[] β€” accesses the specified character (public member function)
  • at β€” accesses the specified character with bounds checking (public member function)
  • front β€” accesses the first character (public member function)
  • back β€” accesses the last character (public member function)
  • data β€” returns a pointer to the first character of a view (public member function)

Capacity

  • size β€” returns the number of characters (public member function)
  • length β€” (public member function), listed right under size's count description
  • max_size β€” returns the maximum number of characters (public member function)
  • empty β€” checks whether the view is empty (public member function)

Modifiers

  • remove_prefix β€” shrinks the view by moving its start forward (public member function)
  • remove_suffix β€” shrinks the view by moving its end backward (public member function)
  • swap β€” swaps the contents (public member function)

Operations

  • copy β€” copies characters (public member function)
  • substr β€” returns a substring (public member function)
  • compare β€” compares two views (public member function)
  • starts_with (C++20) β€” checks if the string view starts with the given prefix (public member function)
  • ends_with (C++20) β€” checks if the string view ends with the given suffix (public member function)
  • contains (C++23) β€” checks if the string view contains the given substring or character (public member function)
  • find β€” find characters in the view (public member function)
  • rfind β€” find the last occurrence of a substring (public member function)
  • find_first_of β€” find first occurrence of characters (public member function)
  • find_last_of β€” find last occurrence of characters (public member function)
  • find_first_not_of β€” find first absence of characters (public member function)
  • find_last_not_of β€” find last absence of characters (public member function)

🌟 Constants 🌟

  • npos β€” special value. The exact meaning depends on the context [static] (public static member constant)

βš”οΈ Non-member functions βš”οΈ

The comparison crew is all here:

  • operator==
  • operator!=
  • operator<
  • operator>
  • operator<=
  • operator>=
  • operator<=> β€” lexicographically compares two string views

The exact status-tag stack on that block, in order, is giving chaotic table energy 😭:

(C++17)            (function template)
(removed in C++20)
(removed in C++20)
(removed in C++20)
(removed in C++20)
(removed in C++20)
(C++20)

πŸ“Ί Input/output πŸ“Ί

  • operator<< (C++17) β€” performs stream output on string views (function template)

πŸŽ€ Literals πŸŽ€

Defined in inline namespace std::literals::string_view_literals.

  • operator""sv (C++17) β€” creates a string view of a character array literal (function)

🧚 Helper classes 🧚

Hash support for string views is provided by these class template specializations:

  • std::hash<std::string_view> β€” (C++17)
  • std::hash<std::wstring_view> β€” (C++17)
  • std::hash<std::u8string_view> β€” (C++20)
  • std::hash<std::u16string_view> β€” (C++17)
  • std::hash<std::u32string_view> β€” (C++17)
All of that is hash support for string views (C++17) (class template specialization). The version tags sit in this order in the original: (C++17) (C++20) (C++17) (C++17) β€” yes bestie, the formatting is a whole journey πŸ’€.

πŸ¦‹ Helper templates πŸ¦‹

First one, no cap:

template< class CharT, class Traits >

inline constexpr bool
(since C++20)
    ranges::enable_borrowed_range<std::basic_string_view<CharT, Traits>> =
true;

This specialization of ranges::enable_borrowed_range makes basic_string_view satisfy borrowed_range. Periodt.

Second one:

template< class CharT, class Traits >

inline constexpr bool
(since C++20)

    ranges::enable_view<std::basic_string_view<CharT, Traits>> =
true;

This specialization of ranges::enable_view makes basic_string_view satisfy view. Obsessed with the consistency πŸ’œ.

πŸŽ€ Deduction guides (since C++20) πŸŽ€

The page announces Deduction guides (since C++20) here; no extra guide lines are shown in the supplied text before Notes. That's literally the whole section in this part, bestie.

πŸ”₯ 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 table, bestie:

  • __cpp_lib_string_view β€” 201606L (C++17) β€” std::string_view
  • __cpp_lib_string_view β€” 201803L (C++20) β€” ConstexprIterator
  • __cpp_lib_string_contains β€” 202011L (C++23) β€” contains

πŸ“ Example πŸ“

Run this code, bestie:

// Run this code

#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';
    }
}

πŸ“Ί Output

▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─
▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─
▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄
▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀
▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─
▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • LWG 3203 β€” Applied to: C++17. Behavior as published: only pointers, iterators, and references returned from the member functions of basic_string_view might be invalidated. Correct behavior: all pointers, iterators, and references to elements of basic_string_view may be invalidated.

✨ SEE ALSO ✨

okay bestie, you made it to the little link garden at the end 🌸 these are the related pages, and yes they’re all class templates β€” we love a consistent vibe πŸ’…

  • basic_string β€” stores and manipulates sequences of characters πŸ‘‘ (class template)
  • span β€” a non-owning view over a contiguous sequence of objects 🌈 (C++20) (class template)
  • initializer_list β€” creates a temporary array in list-initialization and then (C++11) references it πŸŽ€ (class template)

that’s a wrap on std::basic_string_view(3), bestie πŸ’– source/version http://cppreference.com, date 2024.06.10, mwah 🫢

πŸ“¦ where this page comes from