operator suffix for std::string in c++14

Tags: c++ c++11 c++14
Question!

I install Clang 3.4, and test suffix operator for string literal ""s.

#include <string>

//1. using namespace std; // without compile error.
//2. using std::operator ""s; // or without it compile error, too.
// for success compiles, need 1. or 2. line.

int main()
{
    auto s = "hello world"s; 
}

If I comment 1 and 2 , I get compile error. But I know in big projects 1.-method is bad, and 2.-method is stranger.

QA: Can I use operator ""s without any writing using namespace std and using std::operator ""s ?

By : Khurshid


Answers
Wrapping up the comments:

You have to explicitly pull in those operators with using. This is what is intended by the standard. You should consider to pull in only what you need, either

using namespace std::literals;

or

using namespace std::literals::string_literals;

depending on your needs, the latter being preferable in case of doubt (only pull in what you need, not more). As with all using declarations or directives, you want to limit them to the smallest scope that is practical.

The reason behind the need to pull the operators in explicitly is that the suffixes might be re-used in future extensions in different contexts.



If you found 2nd option strange, that is actually pretty common, you could limit the scope of the "using namespace std" like this:

int main()
{
    using namespace std;
    auto s = "hello world"s; 
}
By : vershov


This video can help you solving your question :)
By: admin