Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::basic_...string_view(3)  C++ Standard Libary std::basic_...string_view(3)

NAME
       std::basic_string::operatorbasic_string_view - std::basic_string::oper-
       atorbasic_string_view

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

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

Parameters
	  (none)

Return value
	  A string view	representing the entire	contents of the	string.

Notes
	  It  is  the programmer's responsibility to ensure that the resulting
       string view does
	  not outlive the string.

	std::string get_string();
	int f(std::string_view sv);

	int x =	f(get_string()); // OK
	std::string_view sv = get_string(); // Bad: holds a dangling pointer

Example
       // 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:
	    show_wstring_size(wcstr);
	}

Output:

	12 code	units
	4 code points

See also
			constructs a basic_string_view
	  constructor	   (public     member	  function     of     std::ba-
       sic_string_view<CharT,Traits>)

http://cppreference.com		  2024.06.10	  std::basic_...string_view(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::u16string::operatorbasic_string_view&sektion=3&manpath=FreeBSD+Ports+15.1.quarterly>

home | help