std::literals::string_literals::operator""s - cppreference.com

From cppreference.com

Defined in header

<string>

std::stringoperator""s(constchar*str,std::size_tlen); (1)(since C++14)
(constexpr since C++20)constexprstd::u8stringoperator""s(constchar8_t*str,std::size_tlen); (2) (since C++20)std::u16stringoperator""s(constchar16_t*str,std::size_tlen); (3)(since C++14)
(constexpr since C++20)std::u32stringoperator""s(constchar32_t*str,std::size_tlen); (4)(since C++14)
(constexpr since C++20)std::wstringoperator""s(constwchar_t*str,std::size_tlen); (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}.

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 any of the following using directives:

usingnamespacestd::literals

usingnamespacestd::string_literals

usingnamespacestd::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

macroValueStdFeature

__cpp_lib_string_udls

201304L

(C++14)User-defined literals for string types Example

Run this code

#include<iostream>#include<string>voidprint_with_zeros(constautonote,conststd::string&s){std::cout<<note;for(constcharc:s)c?std::cout<<c:std::cout<<"₀";std::cout<<" (size = "<<s.size()<<")\n";}intmain(){usingnamespacestd::string_literals;std::strings1="abc\0\0def";std::strings2="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