Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding types to the std namespace

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?

like image 455
Rob Avatar asked Nov 26 '08 14:11

Rob


People also ask

How do you add a namespace to a STD?

Specify the standard namespace, for example: std::printf("example\n"); Use the C++ keyword using to import a name to the global namespace: using namespace std; printf("example\n");

How do I add a class to a namespace in C++?

including functions, classes, structs, etc. here, we added firstNamespace::y. More importantly, you can observe that std is a namespace provided by C++ that contains a lot of useful variables, objects like cout which is of type std::ostream, functions and classeslike std::vector, std::ostream, etc.

Why do we type using namespace std?

So when we run a program to print something, “using namespace std” says if you find something that is not declared in the current scope go and check std. using namespace std; are used. It is because computer needs to know the code for the cout, cin functionalities and it needs to know which namespace they are defined.

What does STD namespace contain?

Explanation: It is known that “std” (abbreviation for the standard) is a namespace whose members are used in the program. So the members of the “std” namespace are cout, cin, endl, etc. This namespace is present in the iostream.


7 Answers

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.

like image 59
Johannes Schaub - litb Avatar answered Oct 13 '22 07:10

Johannes Schaub - litb


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.

like image 36
Rob Walker Avatar answered Oct 13 '22 06:10

Rob Walker


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

like image 38
Lightness Races in Orbit Avatar answered Oct 13 '22 08:10

Lightness Races in Orbit


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.

like image 20
Klaim Avatar answered Oct 13 '22 08:10

Klaim


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?

like image 31
Chris Jefferson Avatar answered Oct 13 '22 08:10

Chris Jefferson


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.

like image 44
jeffery Avatar answered Oct 13 '22 07:10

jeffery


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.

like image 31
Luc Touraille Avatar answered Oct 13 '22 06:10

Luc Touraille