55

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?

5
  • 3
    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 Commented Aug 15, 2016 at 13:19
  • 1
    Are the mismatched quotes in this question a typo or some obscure feature? Commented Aug 15, 2016 at 17:15
  • 3
    @plugwash, it's not obscure, it's new. The comment above yours will explain it for those who don't understand the question. Commented Aug 15, 2016 at 19:35
  • 1
    Well, @underscore_d, I think this is a perfectly suitable question for StackOverflow, with a valid answer that still helps people in 2021. Commented Mar 3, 2021 at 14:43
  • 3
    @PavelŠimerda Yeah, I'm inclined to agree; I don't feel the way I did in 2016. But since my comment has been referenced, I'll leave it, to avoid confusion. Commented Mar 3, 2021 at 15:47

4 Answers 4

59

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)

Sign up to request clarification or add additional context in comments.

10 Comments

@E.Vakili I think you can do using std::string_literals::operator""s;
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.
@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.
Why is this the accepted answer? The question cited to not 'using namespace ...' and yet be able to use it. And I prefer to be explicitly use full namespace scoping such as std::cout << "hello world" << std::endl. How can I use the operator""s without using namespace std::string_literals ??
@ifelsemonkey std::string("foo") or std::string_literals::operator""s("foo", 4)
|
14

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.

1 Comment

Still, this should not be used at namespace scope in a header.
3

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

Comments

3

I would add the "using namespace" thing inside my block of code:

auto get_greetings()
{
    using namespace std::string_literals;
    return "Hello world"s;
}

So, the probability of having operator""s overloaded in some library should be ZERO, because the standard suggests we ordinary people should add a suffix with an underscore. And, since I am adding this using namespace inside my function, no probability of name clashes will occur outside it.

1 Comment

I just ran into this problem using this code in a very simple header: ./src/core/RobotTypes.hpp:12:22: error: expected namespace name 12 | using namespace std::string_literals The solution was to add #include <string>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.