5

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.

PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
  • I much prefer doing `using namespace std::literals;` to doing `using std::operator"" s;` or `using namespace std;` – chris Feb 18 '15 at 17:25
  • 1
    Better yet, just watch [CppCon 2014: Stephan Lavavej "STL Features And Implementation Techniques"](https://www.youtube.com/watch?v=dTeKf5Oek2c), he briefly talks about UDL's in inline namespaces. –  Feb 18 '15 at 17:35

0 Answers0