using std::<type> v.s. using std namespace
使用声明的两种方法是
1 2 | using std::string; using std::vector; |
或
1 | using namespace std; |
号
哪条路更好?
=======下面的满意答案===========
简而言之,在函数中使用声明是无害的,只要提供了本地范围的注意,而全局使用声明是危险的,因为它们可能导致命名空间交叉,从而混淆编译器。
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 #included.
号
这句话归功于乔纳森·韦克利。
这取决于。
如果你想将一个名字注入另一个范围,则使用声明更好。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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); } } |
但有时候你只想要一个名字,这是一个使用指令的名字。可以在功能中本地使用,或者在源文件中全局使用。
除了一个功能机构外,只有当你确切知道什么是安全(I.E.not in a header,where you don't know what's going to be included before or after that header)although many people still frown on this use(read the answers a t why i s using"using space"considered?详情:
ZZU1
a good reason to use a using-directive is where the namespace only contains a small number of names that are kept intentitually segregated,and are designed to be used by using-directive:
1 2 3 4 5 | #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; |
因此,没有一个单一的答案可以说一个人比另一个人更普遍,他们的使用不同,而每一个人在不同的情况下都更好。
关于第一次使用
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.
对我来说,这个建议远远胜过坚持它是坏的,不应该被使用。
法国电力公司和法国电力公司
用一块符号污染全球名称是一个坏主意。你只需要使用
如果你只是简单地使用标准图书馆和其他图书馆,而且永远不会被其他图书馆添加到你的项目中,不管怎样,使用