The fact that the std
namespace is reserved for the standard library does not stop anybody from adding functions to it in one's personal code. Such additions will not become parts of the standard library, of course, but they can be done.
For example, Extending the namespace std - Program-defined types says:
It is undefined behavior to add declarations or definitions to namespace std or to any namespace nested within std, with a few exceptions noted below
...
Program-defined types
...
Program-defined types are non-closure class types or enumeration types that are not part of the C++ standard library and not defined by the implementation, or closure type of non-implementation-provided lambda expressions (since C++11), or instantiation of program-defined specializations.
In the example below, I am adding an overload of function std::to_string()
for a program-defined type C
:
// tested with gcc-11.2.0
#include <iostream>
#include <string>
// some custom class
struct C { int b; float c; };
namespace std {
std::string to_string(const C & c)
{ return "I am: "+std::to_string(c.b)+' '+std::to_string(c.c); }
}
int main(void) {
C c; c.b = 3; c.c = 4.4;
std::cout<<std::to_string(c)<<std::endl;
}
Output:
I am: 3 4.400000
Is what I am doing here illegal from the C++ standard point of view (and I am only able to do it due to an oversight of GCC developers)? Or, is this just an example of code, which many people would consider as a potentially dangerous idea, which is better to avoid?
I am not an expert in C++ seeking an expert opinion.
Update:
Here is what my conclusion is from the expressed opinions. Technically, adding code to the std
namespace can break it since there is a chance that one can change behavior of the code through collisions with the names used to implement it. However, that does not mean that it should not be done if one knows what he/she is doing and has good reasons. Examples:
- One is doing research on the next C++ standard and the only way to test the ideas is to add tons of new code to
std
and check how all the new code fits together and if it collides with anything in the previous version(s) of the standard. - One works on high frequency trading code and came up with a clever idea of how to hack the hell out of the code in
std
to shave off precious nanoseconds in latency, maybe even at the cost of intentionally breaking some of the functionality, and make millions of dollars of additional income. By all means, do it and laugh all the way down to the bank at all the people who wrote "legal" C++ and lost money to you as a result. - One works in computational genetics or high energy physics, and uses C++ to do complex processing of large amounts of data. This person uses class names involving biology or physics terms, which he knows have no chance in hell to collide with anything in
std
. He also knows that his government research funding agency and 99.9% of his research competitors do not give a rat's brownie about C++ standards. He has a sharp deadline to submit his next research funding proposal or to finish a paper or a new data analysis for a conference presentation. And if it helps to save time and have the essential work done by adding something tostd
, do not screw your career - do what you need to do.