Using declarations in private namespaces in header files

好久不见. 提交于 2019-12-25 07:05:55

问题


I have a template class that uses some boost functions in it's methods. Cause this class is template, it's method should be implemented in a header file. I use some using declarations to make 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 safe to use using in a header this way? I don't think somebody would ever use using namespace network::v1::detail; in a .cpp file, so I don't expect functions added to detail namespace would cause any name collisions. Am I wrong?


回答1:


Yes, it is safe. The using declarations only add the boost functions to the detail namespace. You basically answered your own question :-)

Edit: One more thought: Even if somebody were to use your detail namespace and the boost namespace at the same time, for_each etc. would still refer to the same function, so that the alias would not be a problem. If the names then collide with other libraries providing a for_each, you could still disambiguate the usage of the function by prefixing the namespace. But if nobody is usingyour namespace, you are fine.



来源:https://stackoverflow.com/questions/21880363/using-declarations-in-private-namespaces-in-header-files

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