Importing std::tr1 into std - is it legal? Does it improve portability?

房东的猫 提交于 2019-12-10 12:42:47

问题


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?


回答1:


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]




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/10736046/importing-stdtr1-into-std-is-it-legal-does-it-improve-portability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!