std::literals::string_literals::operator""s

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
Defined in header <string>
string operator "" s(const char *str, std::size_t len);
(1) (since C++14)
u16string operator "" s(const char16_t *str, std::size_t len);
(2) (since C++14)
u32string operator "" s(const char32_t *str, std::size_t len);
(3) (since C++14)
wstring operator "" s(const wchar_t *str, std::size_t len);
(4) (since C++14)

Forms a string literal of the desired type.

1) returns std::string{str, len}
2) returns std::u16string{str, len}
3) returns std::u32string{str, len}
4) returns std::wstring{str, len}

Contents

[edit] Parameters

str - pointer to the beginning of the raw character array literal
len - length of the raw character array literal

[edit] Return value

The string literal.

[edit] Notes

These operators are declared in the namespace std::literals::string_literals, where both literals and string_literals are inline namespaces.

std::chrono::duration also defines operator""s, to represent literal seconds, but it is an integer literal: 10s is ten seconds, but "10"s is a string.

[edit] Example

#include <string>
 
int main()
{
    //no need to write 'using namespace std::literals::string_literals'
    using namespace std::string_literals;
 
    std::string s2 = "abc\0\0def"; // forms the string "abc"
    std::string s1 = "abc\0\0def"s; // form the string "abc\0\0def"
}


[edit] See also

constructs a basic_string
(public member function)