你喜欢在C ++中显式命名空间或“使用”吗? [英] Do you prefer explicit namespaces or 'using' in C++?

查看:137
本文介绍了你喜欢在C ++中显式命名空间或“使用”吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++命名空间时,您是否愿意显式命名它们,例如:

  std :: cout< ; Hello,world!\\\
;

或者你更喜欢 using namespace

  using namespace std; 
cout<< Hello,world!\\\
;

如果你喜欢后者,你是否在文件或函数范围声明你的使用? p>

我个人更喜欢显式地命名它们 - 它是更多的打字,但当使用混合的命名空间(例如 std 和<$

解决方案

我总是使用 using namespace for std&促进。其他一些我倾向于使用显式命名空间,除非它被使用太多,它会混乱的代码。



在标题,我从来不使用使用命名空间以避免污染#includeing源的全局命名空间。


When using C++ namespaces, do you prefer to explicitly name them, like this:

std::cout << "Hello, world!\n";

Or do you prefer using namespace:

using namespace std;
cout << "Hello, world!\n";

And if if you prefer the latter, do you declare your usings at file or function scope?

Personally I prefer to explicitly name them - it's more typing but when using a mixture of namespaces (e.g. std and boost) I find it more readable.

解决方案

I always use using namespace for std & boost. Everything else I tend to use an explicit namespace unless it is used so much that it would clutter up the code.

In headers, I never use using namespace to avoid polluting the global namespace of the #including source.

这篇关于你喜欢在C ++中显式命名空间或“使用”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆