Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string view/operator""sv"

From cppreference.com
(char8_t)
(improve formatting)
Line 3: Line 3:
 
{{dcl begin}}
 
{{dcl begin}}
 
{{dcl header | string_view}}
 
{{dcl header | string_view}}
 +
{{dcl namespace | std::literals::string_view_literals}}
 
{{dcl | num=1 | since=c++17 | 1=
 
{{dcl | num=1 | since=c++17 | 1=
constexpr string_view operator "" sv(const char* str, size_t len) noexcept;
+
constexpr string_view
 +
    operator "" sv(const char* str, size_t len) noexcept;
 
}}
 
}}
 
{{dcl | num=2 | since=c++20 | 1=
 
{{dcl | num=2 | since=c++20 | 1=
constexpr u8string_view operator "" sv(const char8_t* str, size_t len) noexcept;
+
constexpr u8string_view
 +
    operator "" sv(const char8_t* str, size_t len) noexcept;
 
}}
 
}}
 
{{dcl | num=3 | since=c++17 | 1=
 
{{dcl | num=3 | since=c++17 | 1=
constexpr u16string_view operator "" sv(const char16_t* str, size_t len) noexcept;
+
constexpr u16string_view
 +
    operator "" sv(const char16_t* str, size_t len) noexcept;
 
}}
 
}}
 
{{dcl | num=4 | since=c++17 | 1=
 
{{dcl | num=4 | since=c++17 | 1=
constexpr u32string_view operator "" sv(const char32_t* str, size_t len) noexcept;
+
constexpr u32string_view
 +
    operator "" sv(const char32_t* str, size_t len) noexcept;
 
}}
 
}}
 
{{dcl | num=5 | since=c++17 | 1=
 
{{dcl | num=5 | since=c++17 | 1=
constexpr wstring_view   operator "" sv(const wchar_t* str, size_t len) noexcept;
+
constexpr wstring_view
 +
    operator "" sv(const wchar_t* str, size_t len) noexcept;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}

Revision as of 05:41, 5 November 2019

 
 
 
 
Template:dcl namespace
Defined in header <string_view>
constexpr string_view
    operator "" sv(const char* str, size_t len) noexcept;
(1) (since C++17)
constexpr u8string_view
    operator "" sv(const char8_t* str, size_t len) noexcept;
(2) (since C++20)
constexpr u16string_view
    operator "" sv(const char16_t* str, size_t len) noexcept;
(3) (since C++17)
constexpr u32string_view
    operator "" sv(const char32_t* str, size_t len) noexcept;
(4) (since C++17)
constexpr wstring_view
    operator "" sv(const wchar_t* str, 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}

Contents

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

#include <string_view>
#include <iostream>
 
int main()
{
    using namespace std::literals;
 
    std::string_view s1 = "abc\0\0def";
    std::string_view s2 = "abc\0\0def"sv;
    std::cout << "s1: " << s1.size() << " \"" << s1 << "\"\n";
    std::cout << "s2: " << s2.size() << " \"" << s2 << "\"\n";
}

Possible output:

s1: 3 "abc"
s2: 8 "abc^@^@def"

See also

constructs a basic_string_view
(public member function) [edit]