There's recommendation in C++ community to not to use using namespace std;. But suppose you want to use string literals e.g. auto s = "dummy's;. Not using using namespace std; cause to failed compile. What is the solution?

share|improve this question
    
The solution might be to search for reference info before asking, on sites like cppreference, whose documentation for operator""s shows exactly what you want – underscore_d 3 hours ago
up vote 12 down vote accepted

operator""s is in 2 inlined namespaces in namespace std. It basically looks like this:

namespace std
{
    inline namespace literals
    {
        inline namespace string_literals
        {
            //operator""s implementation
            //...
        }
    }
}

So, to only get the string literals, use using namespace std::string_literals;.

Alternatevely, if you want to include every literal - including the string literals (like s for seconds if you include chrono, ...): using namespace std::literals;.

Depending on the situation, you might also consider using:

using std::string_literals::operator""s;

instead of importing every name from that namespace.

Note that you should still not include it in a header, at global level (but you can do it inside inline or member functions or namespaces you control)

share|improve this answer
    
Why we could not use something like using std::string_literals‌​::s;? – E. Vakili 10 hours ago
1  
@E.Vakili I think you can do using std::string_literals‌​::operator""s; – Rakete1111 10 hours ago
    
@E.Vakili This sounds like a great idea. Unfortunately GCC v6.1.1 is giving me a warning when I do this, it seems to think I am trying to define my own string literal with an illegal name. – Galik 10 hours ago
1  
He CAN use it also in a header, if it is inside a namenspace he has control of. (Not at the global level). Otherwise it will be impossible to write libraries on top of standard functionalities. – Emilio Garavaglia 9 hours ago
2  
@Rakete1111: if you do using namespace X, you use all the names that are part of it, included the ones that the author of that namespace X imported in it. There is no risk of name clash, because it will already clashed. That's part of the contract. You cannot decide what must be inside a namenspace that is not yours. – Emilio Garavaglia 9 hours ago

For string literals you can use:

using namespace std::string_literals;

That will pull about 4 names into the namespace which is fine. But when you do:

using namespace std;

Then you pull in thousands of names, many of which are commonly used in programs like count and time. This can create hard to find bugs from accidentally referring to the wrong thing.

That's not an issue with the string literals.

Also none of the names that using namespace std::string_literals; brings in should interfere with user defined names because user defined string literals must begin with _ (according to the standard) which avoids conflicts.

However you should still avoid using namespace std::string_literals; in the global namespace of a header file because you should not impose any feature on a user that they don't request.

share|improve this answer
    
Still, this should not be used at namespace scope in a header. – Brian 10 hours ago

Above 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 using namespace std::literals, using namespace std::string_literals, and using namespace std::literals::string_literals

Source : std::literals::string_literals::operator""s

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.