Generally, using namespace
in global scope is considered as a bad practice. However, according to cppreference, the identifiers not starting with underscore(_
) are reserved for standard library in the case of user-defined literals.
the identifier to use as the ud-suffix for the user-defined literals that will call this function. Must begin with the underscore _: the suffixes that do not begin with the underscore are reserved for the literal operators provided by the standard library.
Does that mean I can safely do using namespace std::literals;
in global scope?
From the point of view of the standard, when they say that some names are reserved by the standard library, I'd interpret that as no guaranty of defined behavior, if you break the convention. On the other hand, some compilers may not hold you accountable for breaking the prescribed convention. For example, gcc
gives a warning, not an error, for not starting a literal operator identifier with an underscore.
A better formulation here is not whether including
using namespace std::literals;
in global scope is a safe practice, but whether it's a good defensive programming strategy. Putting using namespace foo
in global scope is not defensive programming, because doing so defeats the purpose of the namespace, at least in part.
Now, given your specific codebase, you may never run into problems with doing so. But that's only for you to judge. If you are planning on sharing your code with others, I would suggest programming defensively, so that an unwitting user of your code won't have opportunities to run into unexpected situations (even if they don't follow all the conventions, or if the standard is modified in the future).