Scroll to navigation

std::literals::string_literals::operator""s(3) C++ Standard Libary std::literals::string_literals::operator""s(3)

NAME

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

Synopsis


Defined in header <string>
(since
std::string operator""s(const char *str, C++14)
std::size_t len); (until
C++20)
constexpr std::string operator""s(const char (since
*str, std::size_t len); C++20)
constexpr std::u8string operator""s(const (2) (since
char8_t *str, std::size_t len); C++20)
(since
std::u16string operator""s(const char16_t C++14)
*str, std::size_t len); (until
C++20)
constexpr std::u16string operator""s(const (1) (since
char16_t *str, std::size_t len); C++20)
(since
std::u32string operator""s(const char32_t C++14)
*str, std::size_t len); (3) (until
C++20)
constexpr std::u32string operator""s(const (since
char32_t *str, std::size_t len); (4) C++20)
(since
std::wstring operator""s(const wchar_t *str, C++14)
std::size_t len); (5) (until
C++20)
constexpr std::wstring operator""s(const (since
wchar_t *str, std::size_t len); 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 either


* using namespace std::literals, or
* using namespace std::string_literals, or
* 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: __cpp_lib_string_udls

Example

// Run this code


#include <string>
#include <iostream>


void print_with_zeros(auto const note, std::string const& 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


constructor constructs a basic_string
(public member function)
operator""sv Creates a string view of a character array literal
(C++17) (function)

2022.07.31 http://cppreference.com