使用命名空间认为不好 [英] Using namespace considered bad?

查看:94
本文介绍了使用命名空间认为不好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在较旧的C ++计算机书籍中,你经常会看到


使用命名空间std;


即使在我1996年的Stroustrup副本中也是如此。如今,似乎更好地认为

更有资格使名称更清楚你正在使用什么符号。

是否有任何文章支持这一观点?是使用

命名空间明确错误,或仅仅是风格问题?


如果实现库foo,它会不会使代码

如此清晰只是写使用名称空间foo在

源文件的顶部,使代码更具可读性?


Calum

In older C++ computer books, you''ll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using.
Are there any articles that support this viewpoint? Is "using
namespace" definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code
so much clearer just to write "using namespace foo" at the top of the
source file to make the code much more readable?

Calum

推荐答案

Calum Grant写道:
Calum Grant wrote:
在较旧的C ++计算机书籍中,你经常会看到

使用命名空间std;
甚至在我1996年的Stroustrup副本中。如今,似乎认为更好地限定名称以使其更清楚地使用您所使用的符号。
是否有任何文章支持这一观点?是使用
命名空间明确错误,或仅仅是风格问题?

实现库foo时,如果只编写使用命名空间foo,它会不会使代码更加清晰;在
源文件的顶部,使代码更具可读性?
In older C++ computer books, you''ll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using.
Are there any articles that support this viewpoint? Is "using
namespace" definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code
so much clearer just to write "using namespace foo" at the top of the
source file to make the code much more readable?




经验法则:

''使用命名空间中的''''''''''''''''''''''''''''''''''''''''''''''''''''''''$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ )。

''使用命名空间无论什么''源文件中的所有#include都可以使用

来使代码更具可读性。 />

RC



Rule of thumb:
''using namespace whatever'' in header files is bad because you pull in
the names of the namespace for all following (include) files (which
would defeat the purpose of the namespace).
''using namespace whatever'' after all #include(s) in source files is ok
to "make the code much more readable".

R.C.


Calum Grant写道:
Calum Grant wrote:
在旧的C ++计算机中书籍,你经常会看到

使用命名空间std;

即使在我1996年的Stroustrup副本中也是如此。如今,似乎认为更好地限定名称以使其更清楚地使用您所使用的符号。
是否有任何文章支持这一观点?是使用
命名空间明确错误,或仅仅是风格问题?

实现库foo时,如果只编写使用命名空间foo,它会不会使代码更加清晰;在
源文件的顶部,使代码更具可读性?
In older C++ computer books, you''ll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using.
Are there any articles that support this viewpoint? Is "using
namespace" definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code
so much clearer just to write "using namespace foo" at the top of the
source file to make the code much more readable?




取决于。考虑使用另一个库bar。它定义了一个具有

同名的类(比如说list,例如)。在一个文件中,你总是要在其他地方看看

来查找实际意味着哪个列表。

另外,你不能这样做


使用命名空间foo;

使用命名空间栏;


因为这会导致名称冲突。命名空间基本上用于两个

目的:


- 将组织名称分组以使代码更易理解

- 避免名称冲突


" using namespace"往往会忽略这两个优点,基本上使整个名称空间概念无用。因此,我认为它实际上应该是无用的命名空间。而不是使用命名空间 ;-)



Depends. Consider having another library "bar" that defines a class with the
same name (say "list" e.g.). Within a file, you''d always have to look
somewhere else to find which list is actually meant.
Also, you cannot do

using namespace foo;
using namespace bar;

because that would lead to name clashes. namespaces basically serve two
purposes:

- organizing names into groups to make the code more understandable
- avoiding name clashes

"using namespace" tends to elide both advantages, basically making the whole
concept of namespaces useless. Therefore, I think it should actually be
"useless namespace" instead of "using namespace" ;-)


Rolf Magnus写道:
Rolf Magnus wrote:
Calum Grant写道:

Calum Grant wrote:

在较旧的C ++计算机书籍中,你经常会看到

使用命名空间std;

即使在我1996年的Stroustrup副本中也是如此。如今,似乎认为更好地限定名称以使其更清楚地使用您所使用的符号。
是否有任何文章支持这一观点?是使用
命名空间明确错误,或仅仅是风格问题?

实现库foo时,如果只编写使用命名空间foo,它会不会使代码更加清晰;在
源文件的顶部,使代码更具可读性?

取决于。考虑使用另一个库bar。它定义了一个具有相同名称的类(比如说list)。在一个文件中,你总是必须在其他地方查找确定哪个列表实际意味着什么。
另外,你不能使用命名空间foo;
>使用命名空间栏;

因为这会导致姓名冲突。
In older C++ computer books, you''ll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using.
Are there any articles that support this viewpoint? Is "using
namespace" definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code
so much clearer just to write "using namespace foo" at the top of the
source file to make the code much more readable?

Depends. Consider having another library "bar" that defines a class with the
same name (say "list" e.g.). Within a file, you''d always have to look
somewhere else to find which list is actually meant.
Also, you cannot do

using namespace foo;
using namespace bar;

because that would lead to name clashes.




是的,名字冲突很有帮助 - 它如果

的名字默默地解决了一个或另一个,那将会更糟。


我只是没有看到问题。如果发生冲突则会出现错误,如果没有冲突,则为
,没有问题。因此,如果你发生冲突,你需要做的就是确定名称,这一切都有效。

命名空间基本上用于两个目的:

- 将名称组织成组以使代码更易理解
- 避免名称冲突

using namespace往往会忽略这两个优点,基本上使命名空间的整个概念变得毫无用处。因此,我认为它实际上应该是无用的命名空间。而不是使用命名空间 ; - )



Yes, it''s pretty helpful that names clash - it would be far worse if the
name silently resolved to one or the other.

I just don''t see the problem. If there''s a clash you get an error, and
if there is no clash, there is no problem. So if you get a clash, all
you need to do is qualify the name and it all works again.
namespaces basically serve two
purposes:

- organizing names into groups to make the code more understandable
- avoiding name clashes

"using namespace" tends to elide both advantages, basically making the whole
concept of namespaces useless. Therefore, I think it should actually be
"useless namespace" instead of "using namespace" ;-)




我只能谈谈自己的经历 - 我经常把使用

命名空间mylib;在我的.cpp文件的顶部,只有很少的不良影响。

虽然现在我正在重新评估这个选择。


是否正在使用foo :: list ;"优于using namespace foo; ?

Calum



I can only relate my own experiences - I have regularly put "using
namespace mylib;" at the top of my .cpp files with very few bad effects.
Though now I am reevaluating this choice.

Is "using foo::list;" better than "using namespace foo;" ?

Calum


这篇关于使用命名空间认为不好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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