简体   繁体   English

隐藏第三方名称空间是否有危险?

[英]Is shadowing 3rd party namespaces dangerous?

I've often found myself in a situation where a 3rd party library could use some features or utilities that do not currently exist. 我经常发现自己处于第三方库可能使用某些当前不存在的功能或实用程序的情况。 In writing those companion utilties, the question arises as to which namespace to put them in. 在编写那些伴随实用程序时,出现了将它们放入哪个名称空间的问题。

I've picked a convention to shadow the 3rd party's namespace within my own, but I've not entirely convinced myself that there aren't unwanted repercussions lurking. 我选择了一个约定将第3方的名称空间隐藏在自己的约定中,但是我还没有完全使自己确信没有潜伏的反响。

Example utility header: 实用程序头示例:

#include <third_party/Thing.hpp>

namespace my_namespace
{
    namespace third_party
    {
        typedef ::third_party::Thing<int,3> Thing3i
    }
}

So the question is: Are there any significant negative consequences of doing this? 所以问题是: 这样做是否有任何重大的负面后果?

I don't particularly see anything wrong but I would caution that you do not pollute your namespace with the 3rd party one in case of ambiguity or bloat, there is a good answer to this issue with whether it is a good idea to nest namespaces: C++ namespaces advice , Nested NameSpaces in C++ and here Usage of namespaces in c++ . 我没有特别发现任何错误,但是我要提醒您,如果歧义或膨胀,请不要用第三方污染您的名称空间,对于嵌套名称空间是否是个好主意,对于此问题有一个很好的答案: C ++名称空间建议C ++中的 嵌套名称空间以及此处c ++中名称空间的用法

In general it should be safe and I would restrict using namespace third_party usage to just cpp files, otherwise if you were to declare using namespace third_party in your header then the code that #include s your header will accidentally acquire the third party namespace unless you really want that to happen. 通常,它应该是安全的,并且我将using namespace third_party使用限制为仅cpp文件,否则,如果您要在标头中声明using namespace third_party ,则#include s标头的代码将意外获取第三方命名空间,除非您确实希望那发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM