std::basic_..._string_view(3)
β¨ std::basic_..._string_view(3) β¨
okay so this is the constructor page for std::basic_string_view, straight from the C++ Standard Libary (yes bestie, that spelling is exactly what the manual says π). it's giving βseven overloads walk into a group chat,β and every single one is iconic in its own way. let's get into it π
πΈ NAME πΈ
std::basic_string_view::basic_string_view β std::basic_string_view::basic_string_view
yes, the name is literally the same thing twice. main character energy, fr fr.
π Synopsis π
constexpr basic_string_view() noexcept;
(1) (since C++17)
constexpr basic_string_view( const basic_string_view& other )
noexcept
(2) (since C++17)
= default;
constexpr basic_string_view( const CharT* s, size_type count );
(3) (since C++17)
constexpr basic_string_view( const CharT* s );
(4) (since C++17)
template< class It, class End >
(5) (since C++20)
constexpr basic_string_view( It first, End last );
template< class R > constexpr explicit basic_string_view( R&& r );
(6) (since C++23)
constexpr basic_string_view( std::nullptr_t ) = delete;
(7) (since C++23)
the little numbers in parentheses are overload IDs, iykyk. now here's the lowdown on each one:
- Default constructor. Constructs an empty
std::basic_string_view. After construction,data()is equal tonullptr, andsize()is equal to0. - Copy constructor. Constructs a view of the same content as
other. After construction,data()is equal toother.data(), andsize()is equal toother.size(). - Constructs a view of the first
countcharacters of the character array starting with the element pointed bys.scan contain null characters. The behavior is undefined if[s, s + count)is not a valid range (even though the constructor may not access any of the elements of this range). After construction,data()is equal tos, andsize()is equal tocount. - Constructs a view of the null-terminated character string pointed to by
s, not including the terminating null character. The length of the view is determined as if byTraits::length(s). The behavior is undefined if[s, s + Traits::length(s))is not a valid range. After construction,data()is equal tos, andsize()is equal toTraits::length(s). - Constructs a
std::basic_string_viewover the range[first, last). The behavior is undefined if[first, last)is not a valid range, ifItdoes not actually modelcontiguous_iterator, or ifEnddoes not actually modelsized_sentinel_forforIt. After construction,data()is equal tostd::to_address(first), andsize()is equal tolast - first.
This overload participates in overload resolution only if
Itsatisfiescontiguous_iterator,Endsatisfiessized_sentinel_forforIt,std::iter_value_t<It>andCharTare the same type, andEndis not convertible tostd::size_t.
- Constructs a
std::basic_string_viewover the ranger. After construction,data()is equal toranges::data(r), andsize()is equal toranges::size(r).
This overload participates in overload resolution only if
std::remove_cvref_t<R>is not the same type asstd::basic_string_view,Rmodelscontiguous_rangeandsized_range,ranges::range_value_t<R>andCharTare the same type,Ris not convertible toconst CharT*, and- Let
dbe an lvalue of typestd::remove_cvref_t<R>,d.operator ::std::basic_string_view<CharT, Traits>()is not a valid expression.
std::basic_string_viewcannot be constructed fromnullptr.
bestie, those βundefined behaviorβ bits are not a vibe check you wanna fail β do not test them π
π Parameters π
otherβ another view to initialize the view withsβ pointer to a character array or a C string to initialize the view withcountβ number of characters to include in the viewfirstβ iterator to the first character of the sequencelastβ iterator past the last character of the sequence or another sentinelrβ a contiguous range that contains the sequence
β Complexity β
1-3,5,6)β Constant.4)β Linear in length ofs.
π¦ Example π¦
// Run this code
#include <array>
#include <iomanip>
#include <iostream>
#include <string>
#include <string_view>
int main()
{
std::string cppstr = "Foo";
std::string_view cppstr_v(cppstr); // overload (2), after
// std::string::operator
// string_view
std::cout << "1) cppstr_v: " << std::quoted(cppstr_v) << '\n';
char array[3] = {'B', 'a', 'r'};
std::string_view array_v(array, std::size(array)); // overload (3)
std::cout << "2) array_v: " << std::quoted(array_v) << '\n';
const char* one_0_two = "One\0Two";
std::string_view one_two_v{one_0_two, 7}; // overload (3)
std::cout << "3) one_two_v: \"";
for (char c : one_two_v)
std::cout << (c != '\0' ? c : '?');
std::cout << "\", one_two_v.size(): " << one_two_v.size() << '\n';
std::string_view one_v{one_0_two}; // overload (4)
std::cout << "4) one_v: " << std::quoted(one_v) << ", one_v.size(): "
<< one_v.size() << '\n';
constexpr std::wstring_view wcstr_v = L"xyzzy"; // overload (4)
std::cout << "5) wcstr_v.size(): " << wcstr_v.size() << '\n';
std::array ar = {'P', 'u', 'b'};
std::string_view ar_v(ar.begin(), ar.end()); // overload (5), C++20
std::cout << "6) ar_v: " << std::quoted(ar_v) << '\n';
// std::string_view ar_v2{ar}; // overload (6), OK in C++23
// std::cout << "ar_v2: " << std::quoted(ar_v2) << '\n'; // ar_v2: "Pub"
[[maybe_unused]] auto zero = [] { /* ... */ return nullptr; };
// std::string_view s{zero()}; // overload (7), won't compile since C++23
}
Output:
1) cppstr_v: "Foo"
2) array_v: "Bar"
3) one_two_v: "One?Two", one_two_v.size(): 7
4) one_v: "One", one_v.size(): 3
5) wcstr_v.size(): 5
6) ar_v: "Pub"
π See also π
operator=β assigns a view (public member function)- constructor β constructs a
basic_string(public member function ofstd::basic_string<CharT,Traits,Allocator>)
that's the whole vibe, bestie β source/version http://cppreference.com, date 2024.06.10, and yes the manual name is still std::basic_..._string_view(3). go build something iconic πβ¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms