将std :: tr1导入std-是否合法?会提高便携性吗? [英] Importing std::tr1 into std - is it legal? Does it improve portability?
问题描述
我有C ++ 03代码,如下所示:
I have C++03 code that looks like this:
#include <boost/tr1/unordered_map.hpp>
...
std::tr1::unordered_map<std::string, int> mystuff;
...
我开始怀疑,如果以后将代码转换为C ++ 11时会遭受痛苦,(我想)它没有std::tr1::unordered_map
但却具有std::unordered_map
.所以我想出了以下技巧:
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!
...
是否合法(也许禁止将物料导入到std
中)?它将使与C ++ 11代码的移植/互操作变得更容易吗?
Is it legal (maybe importing stuff into std
is forbidden)? Will it make it easier to port/interoperate with C++11 code?
推荐答案
您不应触摸std
命名空间:即使现在可以使用,也可能会在以后导致严重的头痛(使用新版本的编译器时,不同的编译器等).
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).
更新:引用标准(C ++ 2003,第17.4.3.1节保留名称")(找到
Update: Quote from the standard (C++ 2003, Section 17.4.3.1 "Reserved names") (found here):
对于C ++程序,除非另有说明,否则将声明或定义添加到名称空间std或名称空间std中的名称空间未定义.程序可以将任何标准库模板的模板专业化添加到名称空间std.除非声明依赖于用户定义的外部链接类型,并且除非该专业化满足原始模板的标准库要求,否则标准库模板的这种专业化(全部或部分)会导致未定义的行为. [强调我的]
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]
这篇关于将std :: tr1导入std-是否合法?会提高便携性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!