您更喜欢显式命名空间还是“使用”命名空间? 在 C++ 中?

发布于 2024-07-06 17:49:51 字数 375 浏览 8 评论 0原文

使用 C++ 命名空间时,您是否更喜欢显式命名它们,如下所示:

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

或者您更喜欢 using namespace

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

如果您更喜欢后者,您是否在文件或函数作用域中声明您的 using?

就我个人而言,我更喜欢明确地命名它们 - 它需要更多的输入,但是当使用混合命名空间(例如 stdboost)时,我发现它更具可读性。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

·深蓝 2024-07-13 17:49:51

我总是使用明确的。 写 std 不会伤害我,我清楚地看到它来自哪里。 当您需要处理一些遗留项目并需要维护它自己的“字符串”、“向量”等时,它非常有用。 代码携带的信息越多越好。

I use always explicit ones. Writing std does not hurt me and I clearly see where is it from. It's useful when you have some legacy project to take care of with it's own "strings", "vectors" etc to maintain. The more information the code carries with it, the better.

橙幽之幻 2024-07-13 17:49:51

额外的打字不是这里的问题。 显式限定名称的问题是视觉混乱。 让我们面对现实吧,C++ 语法很混乱。 没有必要通过不必要地使名称更长并在代码中大量使用 :: 来使情况变得更糟。

我同意 Jeff Atwood 的观点:最好的代码就是根本没有代码。 这是千真万确的。

命名空间导入是减少混乱的好方法,而且没有任何缺点:只要将打开的命名空间的范围缩小到单个编译单元1,即使出现名称冲突,也可以轻松解决。

为什么显式名称(一般来说)应该更具可读性对我来说一直是个谜。 读者通常应该足够了解代码,能够推断出语义。 如果不是,则代码无论如何都需要修复。


1)推论:标题中没有using

Extra typing is not the issue here. The problem with explicitly qualified names is the visual clutter. Let's face it, C++ syntax is untidy. No need to make this worse by needlessly making names longer and sprinkling the code generously with ::s.

I'm with Jeff Atwood: The Best Code is No Code At All. This is so true.

Namespace imports are a great way of reducing clutter with no drawback: As long as the scope of opened namespaces is reduced to a single compilation unit1, name conflicts, should they appear, can be resolved easily.

Why explicit names should (in general) be more readable has always been a mystery to me. The readers should generally know the code good enough to be able to deduce semantics. If they aren't, the code needs fixing anyway.


1) Corollary: no using in headers!

乞讨 2024-07-13 17:49:51

我总是使用 using namespace 作为 std & 。 促进。 其他一切我都倾向于使用显式命名空间,除非它使用得太多以至于会使代码变得混乱。

在标头中,我从不使用 using namespace 以避免污染 #include 源的全局命名空间。

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.

泛泛之交 2024-07-13 17:49:51

我的一般规则是始终在标头中显式使用名称空间,并且通常在代码中使用 using 。 前者的原因是为了明确定义的每个部分所使用的内容,而后者的原因是如果有必要,可以轻松使用另一个名称空间的替换。 即,如果我们想开始使用 foo::string 而不是 std::string,我们只需要更新标头和 using 语句,而不是在代码中用 foo::string 替换 std::string 的每个实例。

当然,这对于驻留在 std:: 命名空间中的类不太有用,因为即使替换一个类,您仍然可能在 std 中使用其他类,并且可能会遇到歧义问题,但这只是一个示例。

My general rule is always explicitly use the namespace in headers, and usually use using in the code. The reason for the former is to make it explicitly clear in every part of the definition what is being used, and the reason for the latter is that it makes it easy to use replacements from another namespace if that becomes necessary. i.e. if we want to start using foo::string instead of std::string we just need to update the header and the using statement rather than replacing every instance of std::string with foo::string in the code.

Of course, that is less useful for classes that reside in the std:: namespace, since even if you replace one class you're still likely to use others in std, and may run into ambiguity issues, but that was just an example.

软的没边 2024-07-13 17:49:51

usingusing namespace 对于使代码更具可读性非常有用 - 消除混乱。

但在任何情况下,如果很难找出符号的来源,我都会拒绝导入它的整个命名空间。

我尝试限制导入的命名空间的范围:

void bar() {

   // do stuff without vector

   { using std::vector;
      // do stuff with vector
   }

   // do stuff without vector
}

对于“众所周知的”库,例如 std,我敢于使用 using namespace std。 有理由相信阅读此代码的每个人都知道这些符号。

作为旁注,using 关键字还用于指示派生类还导出其超类的重载成员。

class A {
  void f( A  );
  void f( bool );
};

class B : public A {
  using A::f; // without this, we get a compilation error in foo()
  void f(bool);
};

void foo() {
  B b;
  b.f( A() ); // here's a compilation error when no `using` is used in B
}

using and using namespace are Very Very useful to render the code more readable - remove clutter.

But in any case where it makes it harder to find out where a symbol comes from, I refuse importing it's whole namespace.

I try to limiit the scope of the imported namespaces:

void bar() {

   // do stuff without vector

   { using std::vector;
      // do stuff with vector
   }

   // do stuff without vector
}

For "generally known" libraries, like std, I would dare using using namespace std. There is reason to believe everyone reading this code knows these symbols.

As a sidenote, the using keyword is also used to indicate that a derived class also exports the overloaded members of its superclass.

class A {
  void f( A  );
  void f( bool );
};

class B : public A {
  using A::f; // without this, we get a compilation error in foo()
  void f(bool);
};

void foo() {
  B b;
  b.f( A() ); // here's a compilation error when no `using` is used in B
}
半夏半凉 2024-07-13 17:49:51

我只在存在一些歧义时才使用显式名称空间。 它更具可读性,但额外的输入太乏味,并且您必须假设其他开发人员对标准库有基本的熟悉程度。

我拼写命名空间的唯一情况是当我只使用它一次或两次时,例如添加快速调试语句,或者如果我正在使用一些非标准库。

我通常在文件作用域中声明命名空间,但如果您要混合命名空间,则将声明放在更靠近其使用位置(在函数作用域中)可能是有意义的。

I only use explicit namespaces when there's some ambiguity. It is more readable, but the extra typing is too tedious, and you have to assume other developers have a baseline level of familiarity with standard libraries.

The only other times I spell out a namespace are when I'm only using it once or twice, like adding in a quick debug statement, or if I'm using some non-standard library.

I typically declare the namespace at file scope, but if you're mixing namespaces it might make sense to put the declaration closer to the point where it's used, in function scope.

浅浅 2024-07-13 17:49:51

using 在函数作用域,或者如果函数非常小(通常是),只需显式命名空间

using at function scope, or if the function is very small (often is), just explicit namespace

习ぎ惯性依靠 2024-07-13 17:49:51

我倾向于在 .cpp 文件顶部显式导入所需的名称,因此...

使用 std::cout;
使用 std::endl;

等等...

这样我就可以控制我使用的名称,并且很容易看到它们来自哪里,并且代码在使用时不会混乱。

在极少数情况下,我使用来自不同命名空间的两个名称,我在使用时完全限定它们。

我总是在标头中使用完全限定名称,并且几乎不在任何地方使用“using namespace x”...

I tend to explicitly import the names that I need at the top of the .cpp file, so...

using std::cout;
using std::endl;

etc...

That way I have control over the names that I use and it's easy to see where they come from and the code isn't cluttered at the point of use.

In the rare cases where I am using two names from different namespaces I fully qualify them at the point of use.

I always use fully qualified names in headers and hardly ever use 'using namespace x' anywhere...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文