19

Is it acceptable to add types to the std namespace. For example, I want a TCHAR-friendly string, so is the following acceptable?

#include <string>

namespace std
{
    typedef basic_string<TCHAR> tstring;
}

Or should I use my own namespace?

7 Answers 7

20

Only specializations are allowed. So for example, you are allowed to specialize std::numeric_limits for your type. And this of course must happen in namespace std::. But your typedef isn't a specialization so that's causing undefined behavior.

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

8 Comments

According to this answer, swap implementations shouldn't go into std any more, as algorithms using swap should rely on argument-dependent lookup.
@MvG that answer is right. My example isn't and wasn't good. I will replace it.
Why is numeric_limits any different from swap in this regard?
@DanNissenbaum i don't know the rules for swap. If it is not allowed to specialize it, that may be because ADL provides a superior alternative
@Dan numeric_limits is not a function template, so it will not apply. You can write template<typename T> struct numeric_limits<MyNumberWrapper<T>> { ... }; perfectly fine.
|
19

[C++11: 17.6.4.2.1/1]: The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

Comments

18

No ... part of the point of a namespace is to prevent name collisions on upgrade.

If you add things to the std namespace, then your code might break with the next release of the library if they decide to add something with the same name.

1 Comment

It also violates the standard, afaik. The std namespace is sacred. (Apart from specializations of existing std functions) :)
3

You should use your own namespace as adding code to the standard library will only confuse the users that will look online for informations about that addition.

All that is in std should be only the standard library and nothing else.

3 Comments

I'm not convinced it will confuse users - in fact they may expect a std::basic_string type to be in std. Tough call I think.
No, Klaim is correct. You should not add anything to the std namespace. Users may expect basic_string to be in std, but it is NOT part of the std library and they will not find anything on it in the published documentation for a given std implementation. The correct way is to use your own ns.
I had to look this one up. basic_string is actually defined as part of the std namespace on page 384 of ISO/IEC 14882:1998.
2

Officially, the standard says that's "undefined behaviour", and all kinds of nasty things can happen.

In practice, it will work fine, but you still shouldn't do it. What does it buy you, other than confusing people that something is provided by the compiler?

Comments

2

I totally agree with other answers saying that you should put your types in your own namespace to avoid unfortunate name collisions.

However, I wanted to precise that sometimes, you can (and should !) add stuff in the std namespace. This is the case for template specializations of the std::swap method for example, which are used to provide a uniform way to swap objects. For more information on this matter, you can read about the non-throwing swap idiom.

Comments

2

This is an interesting question because it's completely subjective to the project and the engineers' accepted coding standards.

For a single programmer, why not... just be careful.

For teams, make a standard...

For a cross-platform project, hell yeah.

Otherwise, nawdawg.

1 Comment

It's not subjective, the standard disallows it. See the other answers.

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.