ikh
ikh

Reputation: 10417

Is `using namespace std::literals` safe?

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?

Upvotes: 11

Views: 3414

Answers (2)

MSalters
MSalters

Reputation: 179799

These user-defined literals are pretty new. You have to understand the Standards body here; they didn't have experience with actual use (unlike say Boost). So they came up with a conservative approach: on one side, the predefined suffixes are in namespace std, on the other side the user-defined literals must start with _.

This indeed leaves an open space in the middle: suffixes that do not start with _ but are defined in the global namespace. As we gain experience with the existing literals, a decision might be made who gets to define such literals.

So, to future-proof your code, you shouldn't cause too much problems for others regardless of what choice is made here in future standards. But does that mean "avoid the using in the global namespace"? No. Each Translation unit gets to make its own choice. In your own .cpp files, do what you like. But you shouldn't affect other Translation units. Therefore the actual rule is:

using namespace std::literals is not safe in headers.

Upvotes: 5

SU3
SU3

Reputation: 5387

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).

Upvotes: 3

Related Questions