Using declarations in private namespaces in header files

I have a template class that uses some boost functions in it. Because this class is a template, its method must be implemented in the header file. I use some using declarations to make the code more readable:

 namespace network { namespace v1 { namespace detail { using boost::phoenix::if_; using boost::for_each; /* some more functions */ template <class T> class Some { public: Some() { for_each(inVector, /* some phoenix code */); } private: vector<int> intVector; }; } template <class T> using Some = detail::Some<T>; } } 

Is it possible to use using in the header this way? I don't think anyone ever used using namespace network::v1::detail; in the .cpp file, so I do not expect functions added to the part namespace to cause any name collisions. I'm wrong?

0
source share
1 answer

Yes, it is safe. Using declarations only adds boost to the part namespace. You basically answered your question :-)

Edit: Another thought: even if someone uses the namespace of your parts and the namespace boost at the same time, for_each , etc. they will still refer to the same function so that the alias is not a problem. If the names then collide with other libraries providing for_each , you can still eliminate the use of this function by prefixing the namespace. But if nothing is using your namespace, you're fine.

+1
source

Source: https://habr.com/ru/post/1271709/


All Articles