应该“使用命名空间标准”使用? [英] should "using namespace std" be used?

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

问题描述

最好包含代码using namespace std;在源代码或

中,std命名空间中的每个关键字都应该由命名空间标记限定,

,例如


std: :cout<< 使用std命名空间 << std :: endl;


我自己我不确定我更喜欢哪个,当然更容易指定

正在使用std命名空间而不是标记

名称空间的每个成员?

Is it best to include the code "using namespace std;" in the source or
should each keyword in the std namespace be qualified by the namespace tag,
such as

std::cout << "using std namespace" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify that
the std namespace is being used instead of tagging each member of the
namespace?

推荐答案

" Pep" < pe*@nowhere.com>在消息中写道

news:e7 ********** @ pop-news.nl.colt.net ...
"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@pop-news.nl.colt.net...
最好包括代码using namespace std;在源代码或
中,std命名空间中的每个关键字都应该由命名空间
标记限定,


std :: cout<< 使用std命名空间 << std :: endl;

我自己不确定我更喜欢哪个,当然更容易指定
使用std命名空间而不是标记每个成员
命名空间?
Is it best to include the code "using namespace std;" in the source or
should each keyword in the std namespace be qualified by the namespace
tag,
such as

std::cout << "using std namespace" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify
that
the std namespace is being used instead of tagging each member of the
namespace?




对于非平凡的代码,使用std命名空间;可能没问题,但我从来没用过它。

然后。


使用std命名空间的问题;是它将所有内容都带入

未命名的命名空间。通常这不会导致问题,直到你尝试

声明一个函数或变量或类与std名称空间中的其他东西

相同。 />

如果你不喜欢做std :: cout std :: endl等...只需将你需要的内容带到未命名的命名空间中。


使用std :: cout;

使用std :: endl;


现在你可以使用

cout<< blah blah <<结束;

没有引进其他所有内容。



For non trivial code, using std namespace; may be okay, but I never use it
then either.

The problem with using std namespace; is that it brings everything into the
unnamed namespace. Usually this doesn''t cause problems, until you try to
declare a function or variable or class with the same name as something else
in the std namespace.

If you don''t really like doing std::cout std::endl etc... just bring what
you need into the unnamed namespace.

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

now you can use
cout << "blah blah" << endl;
without bringing in everything else.


Jim Langston写道:
Jim Langston wrote:
" Pep" ; < pe*@nowhere.com>在消息中写道
新闻:e7 ********** @pop-news.nl.colt.net ...
"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@pop-news.nl.colt.net...
最好是包含代码 using namespace std;"在源代码或
中,std命名空间中的每个关键字都应该由命名空间
标记限定,


std :: cout<< 使用std命名空间 << std :: endl;

我自己不确定我更喜欢哪个,当然更容易指定
使用std命名空间而不是标记每个成员
命名空间?
对于非平凡的代码,使用std命名空间;可能没关系,但我从来没有使用它
然后。

使用std命名空间的问题;就是它将所有内容都带入了未命名的命名空间。通常这不会引起问题,直到你试图声明一个函数或变量或类与std名称空间中的其他东西同名。
Is it best to include the code "using namespace std;" in the source or
should each keyword in the std namespace be qualified by the namespace
tag,
such as

std::cout << "using std namespace" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify
that
the std namespace is being used instead of tagging each member of the
namespace?
For non trivial code, using std namespace; may be okay, but I never use it
then either.

The problem with using std namespace; is that it brings everything into
the
unnamed namespace. Usually this doesn''t cause problems, until you try to
declare a function or variable or class with the same name as something
else in the std namespace.




这是我选择的地方。鉴于所有c ++程序员都知道std命名空间并期望它提供标准的c / c ++

enities,我们不应该将我们的覆盖放在应用程序中特定的

命名空间然后用命名空间标签限定例程的使用?


ie


namespace foo

{

int atol(const char * val)

{

return(std :: atol(val) )* 100);

}

}


cout<< foo :: atol(" 12")<< endl;


这是非常明显地从不同的名称空间调用atol而不是std和

a项目的新开发人员我会立即怀疑

例行程序,并希望查看它的功能。

如果你不喜欢做std :: cout std :: endl等...只需带上什么
你需要进入未命名的命名空间。

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

现在你可以使用
cout<< blah blah <<没有引入其他所有内容。



This is where I dither over the choice. Given that all c++ programmers are
aware of the std namespace and expects it to provide the standard c/c++
enities, shouldn''t we place our overrides in a application specific
namespace and then qualify the use of the routines with the namespace tag?

i.e.

namespace foo
{
int atol(const char* val)
{
return(std::atol(val) * 100);
}
}

cout << foo::atol("12") << endl;

This is very clearly calling atol from a different namespace than std and as
a new developer on the project I would immediately be suspect of the
routine and would want to check out it''s functionality.
If you don''t really like doing std::cout std::endl etc... just bring what
you need into the unnamed namespace.

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

now you can use
cout << "blah blah" << endl;
without bringing in everything else.




然而,作为一个项目的新开发人员,已经记录严重并且已经奠定了
$ b超过几百个源文件的$ b,我可能会想念cout和

endl这样的事实。就这样混合使用导入的

cout,导入的endl和让我们说一个本地声明的atol可能会让你感到困惑,因为你自然会认为std命名空间已经使用了

因此使用std :: atol代替foo :: setw。



Yet, as a new developer on a project that has been badly documented and laid
out over several hundred source files, I might miss the fact that cout and
endl were brought in like this. As such the mixed used of the imported
cout, imported endl and let''s say a locally declared atol might get
confusing as you would naturally assume that the std namespace has been
employed and therefore are using std::atol instead of foo::setw.




Pep < pe*@nowhere.com>在消息中写道

news:e7 ********** @ pop-news.nl.colt.net ...

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@pop-news.nl.colt.net...
最好包括代码using namespace std;在源代码或
中,std命名空间中的每个关键字都应该由命名空间
标记限定,


std :: cout<< 使用std命名空间 << std :: endl;

我自己不确定我更喜欢哪个,当然更容易指定
使用std命名空间而不是标记每个成员
命名空间?
Is it best to include the code "using namespace std;" in the source or
should each keyword in the std namespace be qualified by the namespace
tag,
such as

std::cout << "using std namespace" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify
that
the std namespace is being used instead of tagging each member of the
namespace?



这在常见问题中有所介绍:
http://www.parashift.com/c++-faq-lit....html#faq-27.5


问候,

Sumit。

-

Sumit Rajan< su **** *****@gmail.com>


This is covered in the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-27.5

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>


这篇关于应该“使用命名空间标准”使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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