Question

I have C++03 code that looks like this:

#include <boost/tr1/unordered_map.hpp>
...
std::tr1::unordered_map<std::string, int> mystuff;
...

I started to wonder that i would suffer later if/when i convert my code to C++11, which (i guess) doesn't have std::tr1::unordered_map but has std::unordered_map instead. So i came up with the following hack:

namespace std
{
    using namespace ::std::tr1;
}
...
std::unordered_map<std::string, int> mystuff; // no tr1 now!
...

Is it legal (maybe importing stuff into std is forbidden)? Will it make it easier to port/interoperate with C++11 code?

Was it helpful?

Solution

You should not touch the std namespace: even if it works now, it can cause severe headaches later (with a new version of the compiler, on a different compiler, etc).

Update: Quote from the standard (C++ 2003, Section 17.4.3.1 "Reserved names") (found here):

It is undefined for a C++ program to add declarations or definitions to namespace std or namespaces within namespace std unless otherwise specified. A program may add template specializations for any standard library template to namespace std. Such a specialization (complete or partial) of a standard library template results in undefined behavior unless the declaration depends on a user-defined type of external linkage and unless the specialization meets the standard library requirements for the original template. [emphasis mine]

OTHER TIPS

Importing stuff into ::std is forbidden by C++11 17.6.4.2.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.

I think this question is very similar to what you are asking about.

In particular, I like the answer which says "use autoconf to detect symbols availability and then use conditional defines to alias the right namespace with a given name".

This kind of portability should only be attempted if you have a proof that you cannot support a particular library in a clearer way, and ideally you should surround it with #ifdefs specific to that particular environment.

The point of tr1 was to isolate your std from the stuff in tr1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top