Do you prefer explicit namespaces or #39;using#39; in C++?(你更喜欢显式命名空间还是 C++ 中的“使用?)
问题描述
在使用 C++ 命名空间时,您是否更喜欢显式命名它们,如下所示:
When using C++ namespaces, do you prefer to explicitly name them, like this:
std::cout << "Hello, world!
";
或者你更喜欢使用命名空间:
using namespace std;
cout << "Hello, world!
";
如果你更喜欢后者,你是在文件范围还是函数范围声明你的使用?
And if if you prefer the latter, do you declare your usings at file or function scope?
就我个人而言,我更喜欢明确地命名它们——它的类型更多,但是当使用混合命名空间(例如 std 和 boost)时,我发现它更具可读性.
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.
推荐答案
我总是使用 using namespace for std &促进.其他一切我都倾向于使用显式命名空间,除非它使用得太多以至于会弄乱代码.
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.
在标头中,我从不使用 using namespace 以避免污染#include 源的全局命名空间.
In headers, I never use using namespace to avoid polluting the global namespace of the #including source.
这篇关于你更喜欢显式命名空间还是 C++ 中的“使用"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你更喜欢显式命名空间还是 C++ 中的“使用"?
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
