std::litera...:operator""s(3)
πΈ NAME πΈ
okay bestie, this is the page for std::literals::string_literals::operator""s β and literally the description is the same string again: std::literals::string_literals::operator""s. Iconic. It comes from the C++ Standard Libary manual, section (3), and it's all about forming a string literal of the desired type β¨
β¨ Synopsis β¨
Defined in header <string>.
Here are the overloads, all exact and accounted for:
std::string operator""s( const char* str, std::size_t len );
(1) (since C++14), and (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), and (constexpr since C++20)
std::u32string operator""s( const char32_t* str, std::size_t len );
(4) (since C++14), and (constexpr since C++20)
std::wstring operator""s( const wchar_t* str, std::size_t len );
(5) (since C++14), and (constexpr since C++20)
Forms a string literal of the desired type. The returns are literally:
- Returns
std::string{str, len}. - Returns
std::u8string{str, len}. - Returns
std::u16string{str, len}. - Returns
std::u32string{str, len}. - 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. Obsessed.
π 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, orusing namespace std::string_literals, orusing 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 tea β:
__cpp_lib_string_udlsβ value201304L, std(C++14), feature: User-defined literals for string types.
π Example π
// Run this code
#include <iostream>
#include <string>
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
(C++17)β creates a string view of a character array literal (function).
okay that's the whole vibe, bestie π«Ά source: http://cppreference.com, dated 2024.06.10, and the footer tag is std::litera...:operator""s(3) β periodt π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms