I'm pretty surprised to discover that this code doesn't compile (assuming we're using a C++14 compiler):
std::cout << "hello world!\n"s;
The error showed by ideone is the following:
unable to find string literal operator 'operator""s'
Luckily is pretty simple to fix with a using
statement:
using namespace std::literals::string_literals;
std::cout << "hello world!\n"s; // Compiles!
I'm wondering if there's a reason behind the placement of the standard string user defined literals in a different namespace than the std::string
itself; I have been thinking on it and I cannot figure the reason.
Isn't possible to collide with the operator""s
from std::chrono
because they operate with different types:
auto ten_seconds = 10s; // ten seconds
auto some_string = "some string"s; // some string
I was thinking that the resason could be to not collide with my user defined literals but the standard says that I must preceed them with '_'
underscore:
warning: literal operator suffixes not preceded by '_' are reserved for future standardization
So, I'm pretty surprised, anyone knows the reason behind placing the string literal operator into the std::literals::string_literals
?
Thanks.