Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/operator""s"

From cppreference.com
< cpp‎ | string‎ | basic string
m (rv)
m (Minor fix.)
 
(14 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{cpp/title|n=literals::string_literals::|operator""s }}
+
{{cpp/title|n=literals::string_literals::|operator""s}}
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | string}}
+
{{dcl header|string}}
{{dcl rev multi | num=1
+
{{|num=1|=c++14|=c++20|
| since1=c++14 | dcl1=
+
std::string operator""s( const char* str, std::size_t len );
std::string operator""s(const char *str, std::size_t len);
+
| since2=c++20 | dcl2=
+
constexpr std::string operator""s(const char *str, std::size_t len);
+
 
}}
 
}}
{{dcl | num=2 | since=c++20 | 1=
+
{{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 );
 
}}
 
}}
{{dcl rev multi | num=3
+
{{|num=3|=c++14|=c++20|
| since1=c++14 | dcl1=
+
std::u16string operator""s( const char16_t* str, std::size_t len );
std::u16string operator""s(const char16_t *str, std::size_t len);
+
| since2=c++20 | dcl2=
+
constexpr std::u16string operator""s(const char16_t *str, std::size_t len);
+
 
}}
 
}}
{{dcl rev multi | num=4
+
{{|num=4|=c++14|=c++20|
| since1=c++14 | dcl1=
+
std::u32string operator""s( const char32_t* str, std::size_t len );
std::u32string operator""s(const char32_t *str, std::size_t len);
+
| since2=c++20 | dcl2=
+
constexpr std::u32string operator""s(const char32_t *str, std::size_t len);
+
 
}}
 
}}
{{dcl rev multi | num=5
+
{{|num=5|=c++14|=c++20|
| since1=c++14 | dcl1=
+
std::wstring operator""s( const wchar_t* str, std::size_t len );
std::wstring operator""s(const wchar_t *str, std::size_t len);
+
| since2=c++20 | dcl2=
+
constexpr std::wstring operator""s(const wchar_t *str, std::size_t len);
+
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 34: Line 23:
 
Forms a string literal of the desired type.
 
Forms a string literal of the desired type.
  
@1@ returns {{c|std::string{str, len} }}
+
@1@ {{c|std::string{str, len} }}
@2@ returns {{c|std::u8string{str, len} }}
+
@2@ {{c|std::u8string{str, len} }}
@3@ returns {{c|std::u16string{str, len} }}
+
@3@ {{c|std::u16string{str, len} }}
@4@ returns {{c|std::u32string{str, len} }}
+
@4@ {{c|std::u32string{str, len} }}
@5@ returns {{c|std::wstring{str, len} }}
+
@5@ {{c|std::wstring{str, len} }}
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | str | pointer to the beginning of the raw character array literal}}
+
{{par|str|pointer to the beginning of the raw character array literal}}
{{par | len | length of the raw character array literal}}
+
{{par|len|length of the raw character array literal}}
 
{{par end}}
 
{{par end}}
  
Line 50: Line 39:
  
 
===Notes===
 
===Notes===
These operators are declared in the namespace {{tt|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::string_literals}}, and {{c|using namespace std::literals::string_literals}}.
+
These operators are declared in the namespace {{|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::string_literals}}
 +
{{c|using namespace std::literals::string_literals}}
  
{{lc|std::chrono::duration}} also defines {{tt|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.
+
{{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.
 +
 
 +
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
#include <string>
+
 
#include <iostream>
 
#include <iostream>
 +
 +
 +
 +
 +
 +
 +
 +
 +
  
 
int main()
 
int main()
 
{
 
{
 
     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;
     std::cout << "s1: " << s1.size() << " \"" << s1 << "\"\n";
+
     "s1: "s1
     std::cout << "s2: " << s2.size() << " \"" << s2 << "\"\n";
+
("";
 +
 +
     std::cout << "".() << \n;
 
}
 
}
| p=true
+
|output=
| output=  
+
s1: abc
s1: 3 "abc"
+
s2: 8
s2: 8 "abc^@^@def"
+
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/basic_string/dsc constructor}}
+
{{dsc inc|cpp/string/basic_string/dsc constructor
 +
}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 18:47, 16 December 2024