Difference between revisions of "cpp/string/basic string/operator""s"
From cppreference.com
< cpp | string | basic string
m (→Synopsis: constexpr since.) |
(Added a line break to avoid wrapping the “constexpr since C++20” marks under Cppreference and Cppreference2 skins.) |
||
Line 3: | Line 3: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header|string}} | {{dcl header|string}} | ||
− | {{ | + | {{|num=1|since=c++14|=c++20| |
std::string operator""s( const char* str, std::size_t len ); | std::string operator""s( const char* str, std::size_t len ); | ||
}} | }} | ||
− | {{dcl|num=2|since=c++20| | + | {{dcl|num=2|since=c++20| |
− | constexpr std::u8string operator""s( const char8_t* str, std::size_t len ); | + | constexpr std::u8string operator""s( const char8_t* str, |
+ | std::size_t len ); | ||
}} | }} | ||
− | {{ | + | {{|num=3|since=c++14|=c++20| |
std::u16string operator""s( const char16_t* str, std::size_t len ); | std::u16string operator""s( const char16_t* str, std::size_t len ); | ||
}} | }} | ||
− | {{ | + | {{|num=4|since=c++14|=c++20| |
std::u32string operator""s( const char32_t* str, std::size_t len ); | std::u32string operator""s( const char32_t* str, std::size_t len ); | ||
}} | }} | ||
− | {{ | + | {{|num=5|since=c++14|=c++20| |
std::wstring operator""s( const wchar_t* str, std::size_t len ); | std::wstring operator""s( const wchar_t* str, std::size_t len ); | ||
}} | }} | ||
Line 38: | Line 39: | ||
===Notes=== | ===Notes=== | ||
− | These operators are declared in the namespace {{c|std::literals::string_literals}}, where both {{tt|literals}} and {{tt|string_literals}} are inline namespaces. Access to these operators can be gained with | + | These operators are declared in the namespace {{c|std::literals::string_literals}}, where both {{tt|literals}} and {{tt|string_literals}} are inline namespaces. Access to these operators can be gained with |
− | * {{c|using namespace std::literals}} | + | * {{c|using namespace std::literals}} |
− | * {{c|using namespace std::string_literals}} | + | * {{c|using namespace std::string_literals}} |
− | * {{c|using namespace std::literals::string_literals}} | + | * {{c|using namespace std::literals::string_literals}} |
− | {{lc|std::chrono::duration}} also defines {{ | + | {{lc|std::chrono::duration}} also defines {{|operator""s}} to represent literal seconds, but it is an arithmetic literal: {{c|10.0s}} and {{c|10s}} are ten seconds, but {{c|"10"s}} is a string. |
{{feature test macro|__cpp_lib_string_udls|User-defined literals for string types|value=201304L|std=C++14}} | {{feature test macro|__cpp_lib_string_udls|User-defined literals for string types|value=201304L|std=C++14}} | ||
Line 53: | Line 54: | ||
#include <string> | #include <string> | ||
− | void print_with_zeros( | + | void print_with_zeros(const note, std::string& s) |
{ | { | ||
std::cout << note; | std::cout << note; | ||
Line 64: | Line 65: | ||
{ | { | ||
using namespace std::string_literals; | using namespace std::string_literals; | ||
− | + | ||
std::string s1 = "abc\0\0def"; | std::string s1 = "abc\0\0def"; | ||
std::string s2 = "abc\0\0def"s; | std::string s2 = "abc\0\0def"s; | ||
print_with_zeros("s1: ", s1); | print_with_zeros("s1: ", s1); | ||
print_with_zeros("s2: ", s2); | print_with_zeros("s2: ", s2); | ||
− | + | ||
std::cout << "abcdef"s.substr(1,4) << '\n'; | std::cout << "abcdef"s.substr(1,4) << '\n'; | ||
} | } |
Revision as of 18:46, 16 December 2024
Defined in header <string>
|
||
std::string operator""s( const char* str, std::size_t len ); |
(1) | (since C++14) (constexpr since C++20) |
constexpr std::u8string operator""s( const char8_t* str, std::size_t len ); |
(2) | (since C++20) |
std::u16string operator""s( const char16_t* str, std::size_t len ); |
(3) | (since C++14) (constexpr since C++20) |
std::u32string operator""s( const char32_t* str, std::size_t len ); |
(4) | (since C++14) (constexpr since C++20) |
std::wstring operator""s( const wchar_t* str, std::size_t len ); |
(5) | (since C++14) (constexpr since C++20) |
Forms a string literal of the desired type.
1) Returns std::string{str, len}.
2) Returns std::u8string{str, len}.
3) Returns std::u16string{str, len}.
4) Returns std::u32string{str, len}.
5) Returns std::wstring{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 literal.
Notes
These operators are declared in the namespace std::literals::string_literals, where both literals
and string_literals
are inline namespaces. Access to these operators can be gained with an of the following using directive:
- using namespace std::literals
- using namespace std::string_literals
- using namespace std::literals::string_literals
std::chrono::duration also defines operator""s to represent literal seconds, but it is an arithmetic literal: 10.0s and 10s are ten seconds, but "10"s is a string.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_string_udls |
201304L |
(C++14) | User-defined literals for string types |
Example
Run this code
#include <iostream> #include <string> void print_with_zeros(const auto note, const std::string& s) { std::cout << note; for (const char c : s) c ? std::cout << c : std::cout << "₀"; std::cout << " (size = " << s.size() << ")\n"; } int main() { using namespace std::string_literals; std::string s1 = "abc\0\0def"; std::string s2 = "abc\0\0def"s; print_with_zeros("s1: ", s1); print_with_zeros("s2: ", s2); std::cout << "abcdef"s.substr(1,4) << '\n'; }
Output:
s1: abc (size = 3) s2: abc₀₀def (size = 8) bcde
See also
constructs a basic_string (public member function) | |
(C++17) |
creates a string view of a character array literal (function) |