std::literals::string_view_literals::operator""sv
From cppreference.com
< cpp | string | basic string view
Defined in header <string_view>
|
||
constexpr std::string_view operator "" sv( const char* str, std::size_t len ) noexcept; |
(1) | (since C++17) |
constexpr std::u8string_view operator "" sv( const char8_t* str, std::size_t len ) noexcept; |
(2) | (since C++20) |
constexpr std::u16string_view operator "" sv( const char16_t* str, std::size_t len ) noexcept; |
(3) | (since C++17) |
constexpr std::u32string_view operator "" sv( const char32_t* str, std::size_t len ) noexcept; |
(4) | (since C++17) |
constexpr std::wstring_view operator "" sv( const wchar_t* str, std::size_t len ) noexcept; |
(5) | (since C++17) |
Forms a string view of a character literal.
1) returns std::string_view{str, len}
2) returns std::u8string_view{str, len}
3) returns std::u16string_view{str, len}
4) returns std::u32string_view{str, len}
5) returns std::wstring_view{str, len}
Parameters
str | - | pointer to the beginning of the raw character array literal |
len | - | length of the raw character array literal |
Return value
The string_view literal.
Notes
These operators are declared in the namespace std::literals::string_view_literals
, where both literals
and string_view_literals
are inline namespaces. Access to these operators can be gained with using namespace std::literals, using namespace std::string_view_literals, and using namespace std::literals::string_view_literals.
Example
Run this code
#include <string_view> #include <iostream> #include <typeinfo> void print_all(const std::string_view sw) { for (char c : sw) std::cout << (c == '\0' ? '?' : c); std::cout << '\n'; } int main() { using namespace std::literals; std::string_view s1 = "abc\0\0def"; std::string_view s2 = "abc\0\0def"sv; std::cout << "s1.size(): " << s1.size() << "; s1: "; print_all(s1); std::cout << "s2.size(): " << s2.size() << "; s2: "; print_all(s2); std::cout << "abcdef"sv.substr(1,4) << '\n'; auto value_type_info = []<typename T>(T) { using V = typename T::value_type; std::cout << "sizeof " << typeid(V).name() << ": " << sizeof(V) << '\n'; }; value_type_info( "char A"sv ); value_type_info( L"wchar_t ∀"sv ); value_type_info( u8"char8_t ∆"sv ); value_type_info( u"char16_t ∇"sv ); value_type_info( U"char32_t ∃"sv ); value_type_info( LR"(raw ⊞)"sv ); }
Possible output:
s1.size(): 3; s1: abc s2.size(): 8; s2: abc??def bcde sizeof char: 1 sizeof wchar_t: 4 sizeof char8_t: 1 sizeof char16_t: 2 sizeof char32_t: 4 sizeof wchar_t: 4
See also
(C++17) |
constructs a basic_string_view (public member function) |
(C++14) |
Converts a character array literal to basic_string (function) |