使用std命名空间 [英] Using std Namespace

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

问题描述

对于std命名空间,使用'use'似乎有不同的观点。

There seem to be different views on using 'using' with respect to the std namespace.

有人说使用' using namespace std ',其他说不要而是前缀std函数,将与' std :: '使用,而其他人说使用这样:

Some say use ' using namespace std', other say don't but rather prefix std functions that are to be used with ' std::' whilst others say use something like this:

using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::vector;

用于所有要使用的std函数。

for all the std functions that are to be used.

每个人的利弊是什么?
|

What are the pros and cons of each? |

推荐答案

大多数C ++用户都很高兴看到 std :: string std :: vector 等等。事实上,看到一个原始的向量 std :: vector 或不同的用户定义向量

Most C++ users are quite happy reading std::string, std::vector, etc. In fact, seeing a raw vector makes me wonder if this is the std::vector or a different user-defined vector.

使用命名空间std; 我总是反对。它会将各种名称导入全局命名空间,并可能导致各种不明显的歧义。

using namespace std; I am always against. It imports all sorts of names into the global namespace and can cause all sorts of non-obvious ambiguities.

这些是 std 命名空间:count,sort,find,equal,reverse。有一个名为 count 的局部变量意味着使用命名空间std 不会使您使用计数而不是 std :: count

Here are some common identifiers that are in the std namespace: count, sort, find, equal, reverse. Having a local variable called count means that using namespace std won't enable you to use count instead of std::count.

冲突就像下面这样。想象一下,你是一个初学者,不知道 std :: count 。想象一下,你在< algorithm> 中使用了其他东西,或者它被一个看起来不相关的标题引入。

The classic example of an unwanted name conflict is something like the following. Imagine that you are a beginner and don't know about std::count. Imagine that you are either using something else in <algorithm> or it's been pulled in by a seemingly unrelated header.

#include <algorithm>
using namespace std;

int count = 0;

int increment()
{
    return ++count; // error, identifier count is ambiguous
}

错误通常很长且不友好,因为std :: count是一个具有一些长嵌套类型的模板。

The error is typically long and unfriendly because std::count is a template with some long nested types.

这是确定的,因为std :: count进入全局命名空间,函数count隐藏它。

This is OK though, because std::count goes into the global namespace and the function count hides it.

#include <algorithm>
using namespace std;

int increment()
{
    static int count = 0;
    return ++count;
}

也许稍微令人惊讶的是,这是可以的。导入到声明性范围的标识符出现在公共命名空间中,它们包含它们被定义的位置和它们被导入到的位置。换句话说, std :: count 在全局命名空间中显示为 count ,但只能在 increment

Perhaps slightly surprisingly, this is OK. Identifiers imported into a declarative scope appear in the common namespace that encloses both where they are defined and where they are imported into. In other words, std::count is visible as count in the global namespace, but only inside increment.

#include <algorithm>

int increment()
{
    using namespace std;
    static int count = 0;
    return ++count;
}

出于类似的原因, count 这里有歧义。 使用命名空间std 不会导致 std :: count ,隐藏外部 code>,因为它可能是预期的。 using namespace rule意味着 std :: count 看起来(在 increment function),就好像它在全局范围,即在与 int count = 0; 相同的范围内被声明,因此引起歧义。

And for similar reasons, count is ambiguous here. using namespace std doesn't cause std::count, hide the outer count as it might be expected. The using namespace rule means that std::count looks (in the increment function) as though it was declared at the global scope, i.e. at the same scope as int count = 0; and hence causing the ambiguity.

#include <algorithm>

int count = 0;

int increment()
{
    using namespace std;
    return ++count; // error ambiguous
}

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

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