std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view (3) - Linux Manuals

std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view: std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view


std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view - std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view


operator std::basic_string_view<CharT, Traits>() const noexcept; (since C++17)

Returns a std::basic_string_view, constructed as if by std::basic_string_view<CharT, Traits>(data(), size())



Return value

A string view representing the entire contents of the string.


// Run this code

  #include <iostream>
  #include <string>
  #include <string_view>

  void show_wstring_size(std::wstring_view wcstr_v)
    std::cout << wcstr_v.size() << " code points\n";

  int main()
    std::string cppstr = "ラーメン"; // narrow string
    std::wstring wcstr = L"ラーメン"; // wide string

    // Implicit conversion from string to string_view
    // via std::string::operator string_view:
    std::string_view cppstr_v = cppstr;

    std::cout << cppstr_v << '\n'
              << cppstr_v.size() << " code units\n";

    // Implicit conversion from wstring to wstring_view
    // via std::wstring::operator wstring_view:

    // Warning:
    // It is the programmer's responsibility to ensure that std::string_view
    // does not outlive the pointed-to string!

    std::string_view BAD(std::string("a temporary string")); // holds a dangling pointer!


  12 code units
  4 code points

See also

              constructs a basic_string_view
constructor (public member function of std::basic_string_view<CharT,Traits>)