使用标准命名空间

2022-01-14 00:00:00 namespaces c++

对于 std 命名空间使用 'using' 似乎有不同的看法.

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;

所有要使用的标准函数.

for all the std functions that are to be used.

各有什么优缺点?

推荐答案

大多数 C++ 用户都非常乐意阅读 std::stringstd::vector 等.事实上,看到原始 vector 让我想知道这是 std::vector 还是不同的用户定义的 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.

我一直反对使用 using namespace std;.它将各种名称导入全局命名空间,并可能导致各种不明显的歧义.

I am always against using using namespace std;. 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 的局部变量意味着 using namespace std 不会让您使用 count 而不是 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 在这里是模棱两可的.using namespace std 不会导致 std::count,像预期的那样隐藏外部 count.using namespace 规则意味着 std::count 看起来(在 increment 函数中)好像是在全局范围内声明的,即在与 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
}

相关文章