Those following string user literals 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 using namespace std::literals
, using namespace std::string_literals
, and using namespace std::literals::string_literals
.
#include <codecvt>
#include <iostream>
#include <locale>
#include <string>
int main()
{
using namespace std::literals::string_literals;
std::string s = "hello world"s;
std::u16string s16 = u"hello world"s;
std::u32string s32 = U"hello world"s;
std::wstring ws = L"hello world"s;
std::cout << s << std::endl;
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> utf16conv;
std::cout << utf16conv.to_bytes(s16) << std::endl;
std::wstring_convert<std::codecvt_utf8_utf16<char32_t>, char32_t> utf32conv;
std::cout << utf32conv.to_bytes(s32) << std::endl;
std::wcout << ws << std::endl;
}
Note:
Literal string may containing \0
std::string s1 = "foo\0\0bar"; // constructor from C-string: results in "foo"s
std::string s2 = "foo\0\0bar"s; // That string contains 2 '\0' in its middle