在头文件的私有名称空间中使用声明 [英] Using declarations in private namespaces in header files
问题描述
我有一个模板类,该模板类在其方法中使用一些boost函数.由于此类是模板,因此其方法应在头文件中实现.我使用一些using
声明使代码更具可读性:
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>;
}
}
以这种方式在标头中使用using
是否安全?我认为没有人会在.cpp文件中使用using namespace network::v1::detail;
,所以我不希望添加到详细名称空间的函数会导致任何名称冲突.我错了吗?
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?
推荐答案
是的,它很安全. using声明仅将boost函数添加到detail命名空间.您基本上回答了自己的问题:-)
Yes, it is safe. The using declarations only add the boost functions to the detail namespace. You basically answered your own question :-)
再想一想:即使有人同时使用您的detail命名空间和boost命名空间,for_each
等仍然会引用相同的函数,因此别名不会成为问题.
如果这些名称随后与提供for_each
的其他库冲突,则仍可以通过在名称空间前添加前缀来消除该函数的用法.但是,如果没有人使用using
您的命名空间,您就可以了.
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.
这篇关于在头文件的私有名称空间中使用声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!