问题
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 using
your namespace, you are fine.
来源:https://stackoverflow.com/questions/21880363/using-declarations-in-private-namespaces-in-header-files