简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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