使用 std::<type> v.s。使用 std 命名空间
using std::<type> v.s. using std namespace
使用 using 声明的两种方式是
using std::string;
using std::vector;
或
using namespace std;
哪种方式更好?
using std::string;
和 using std::vector;
.
用一堆符号污染全局命名空间是个坏主意。您也应该只使用 std
命名空间前缀,这样您就知道您正在使用标准库容器。哪个比这两个选项都好 IMO.
如果您只使用标准库而不使用其他任何东西,并且永远不会将任何其他库添加到您的项目中,请务必使用 using namespace std;
- 在这种情况下您觉得更舒服的任何东西。 "never use using namespace std;
" 的约定来自多个其他库定义诸如 string
、vector
等内容的事实。永远不要导入整个命名空间是一种很好的做法,但它不会对您的情况造成困扰。
视情况而定。
如果您想将单个名称注入另一个范围,using-declaration 更好,例如
namespace foolib
{
// allow vector to be used unqualified within foo,
// or used as foo::vector
using std::vector;
vector<int> vec();
template<typename T> struct Bar { T t; };
template<typename T>
void swap(Bar<T>& lhs, Bar<T>& rhs)
{
using std::swap;
// find swap by ADL, otherwise use std::swap
swap(lhs.t, rhs.t);
}
}
但有时您只需要所有名称,这就是 using-directive 所做的。这可以在函数中局部使用,也可以在源文件中全局使用。
将 using namespace
放在函数 body 之外应该只在您确切知道包含什么的地方进行,这样它是安全的(即 而不是 在 header,你不知道在 header 之前或之后会包含什么)尽管很多人仍然不赞成这种用法(详情请阅读 Why is "using namespace std" considered bad practice? 的答案):
#include <vector>
#include <iostream>
#include "foolib.h"
using namespace foo; // only AFTER all headers
Bar<int> b;
使用 using-directive 的一个很好的理由是命名空间只包含少量有意隔离的名称,并且设计供 using-directive 使用:
#include <string>
// make user-defined literals usable without qualification,
// without bringing in everything else in namespace std.
using namespace std::string_literals;
auto s = "Hello, world!"s;
所以没有单一的答案可以说一个比另一个普遍好,它们有不同的用途,每个在不同的情况下都更好。
关于 using namespace
的首次使用,C++ 的创建者 Bjarne Stroustrup 在 C++ 编程语言第 4 版的§14.2.3 中有这样的说法(强调我的):
Often we like to use every name from a namespace without qualification. That can be achieved by providing a using
-declaration for each name from the namespace, but that's tedious and requires extra work each time a new name is added to or removed from the namespace. Alternatively, we can use a using
-directive to request that every name from a namespace be accessible in our scope without qualification. [...]
[...] Using a using
-directive to make names from a frequently used and well-known library available without qualification is a popular technique for simplifying code. This is the technique used to access standard-library facilities throughout this book. [...]
Within a function, a using
-directive can be safely used as a notational convenience, but care should be taken with global using
-directives because overuse can lead to exactly the name clashes that namespaces were introduced to avoid. [...]
Consequently, we must be careful with using
-directives in the global scope. In particular, don't place a using
-directive in the global scope in a header file except in very specialized circumstances (e.g. to aid transition) because you never know where a header might be #include
d.
对我来说,这似乎比坚持认为它不好并且不应该使用要好得多。
使用 using 声明的两种方式是
using std::string;
using std::vector;
或
using namespace std;
哪种方式更好?
using std::string;
和 using std::vector;
.
用一堆符号污染全局命名空间是个坏主意。您也应该只使用 std
命名空间前缀,这样您就知道您正在使用标准库容器。哪个比这两个选项都好 IMO.
如果您只使用标准库而不使用其他任何东西,并且永远不会将任何其他库添加到您的项目中,请务必使用 using namespace std;
- 在这种情况下您觉得更舒服的任何东西。 "never use using namespace std;
" 的约定来自多个其他库定义诸如 string
、vector
等内容的事实。永远不要导入整个命名空间是一种很好的做法,但它不会对您的情况造成困扰。
视情况而定。
如果您想将单个名称注入另一个范围,using-declaration 更好,例如
namespace foolib
{
// allow vector to be used unqualified within foo,
// or used as foo::vector
using std::vector;
vector<int> vec();
template<typename T> struct Bar { T t; };
template<typename T>
void swap(Bar<T>& lhs, Bar<T>& rhs)
{
using std::swap;
// find swap by ADL, otherwise use std::swap
swap(lhs.t, rhs.t);
}
}
但有时您只需要所有名称,这就是 using-directive 所做的。这可以在函数中局部使用,也可以在源文件中全局使用。
将 using namespace
放在函数 body 之外应该只在您确切知道包含什么的地方进行,这样它是安全的(即 而不是 在 header,你不知道在 header 之前或之后会包含什么)尽管很多人仍然不赞成这种用法(详情请阅读 Why is "using namespace std" considered bad practice? 的答案):
#include <vector>
#include <iostream>
#include "foolib.h"
using namespace foo; // only AFTER all headers
Bar<int> b;
使用 using-directive 的一个很好的理由是命名空间只包含少量有意隔离的名称,并且设计供 using-directive 使用:
#include <string>
// make user-defined literals usable without qualification,
// without bringing in everything else in namespace std.
using namespace std::string_literals;
auto s = "Hello, world!"s;
所以没有单一的答案可以说一个比另一个普遍好,它们有不同的用途,每个在不同的情况下都更好。
关于 using namespace
的首次使用,C++ 的创建者 Bjarne Stroustrup 在 C++ 编程语言第 4 版的§14.2.3 中有这样的说法(强调我的):
Often we like to use every name from a namespace without qualification. That can be achieved by providing a
using
-declaration for each name from the namespace, but that's tedious and requires extra work each time a new name is added to or removed from the namespace. Alternatively, we can use ausing
-directive to request that every name from a namespace be accessible in our scope without qualification. [...]
[...] Using ausing
-directive to make names from a frequently used and well-known library available without qualification is a popular technique for simplifying code. This is the technique used to access standard-library facilities throughout this book. [...]
Within a function, ausing
-directive can be safely used as a notational convenience, but care should be taken with globalusing
-directives because overuse can lead to exactly the name clashes that namespaces were introduced to avoid. [...]
Consequently, we must be careful withusing
-directives in the global scope. In particular, don't place ausing
-directive in the global scope in a header file except in very specialized circumstances (e.g. to aid transition) because you never know where a header might be#include
d.
对我来说,这似乎比坚持认为它不好并且不应该使用要好得多。