C ++中的根名称空间编码约定 [英] root namespace coding convention in C++
本文介绍了C ++中的根名称空间编码约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您是否建议使用::
为全局名称空间添加前缀? (例如,以::std::cout
代替std::cout
)为什么?使用C ++编译器解析起来更快吗?
Would you recommand prefixing global namespaces with ::
? (for instance ::std::cout
instead of std::cout
) Why? Is it faster to parse for the C++ compiler?
谢谢.
推荐答案
只有这样做才能消除歧义.
Only do this to disambiguate.
我有一段代码是必需的,因为我在名称空间X
中,该名称空间具有标准差功能-std
.每当我想访问std
名称空间时,都需要使用::std
,因为否则编译器会认为我是在指上述功能.
I have a piece of code where this is necessary since I’m in a namespace X
which has a function for a standard deviation – std
. Whenever I want to access the std
namespace, I need to use ::std
because otherwise the compiler will think that I am referring to said function.
具体示例:
namespace X {
double std(::std::vector<double> const& values) { … }
void foo(::std::vector<double> const& values) {
::std::cout << std(values) << ::std::endl;
}
}
这篇关于C ++中的根名称空间编码约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文