使用“std”的标准惯例是
对于使用任何名称空间,以下哪一个是首选约定?
using namespace std;
或者
using std::cin;
using std::cout;
?
在代码中需要时调用该函数
std::cout<<"Hello World!"<<std::endl;
Exact Duplicate: Do you prefer explicit namespaces or ‘using’ in C++?
Which of these is a preferred convention for using any namespace?
using namespace std;
or
using std::cin;
using std::cout;
or
calling the function as and when required in the code?
std::cout<<"Hello World!"<<std::endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
此处给出了非常好的解释。
第一种风格,即使用命名空间,无论什么都违背了命名空间的全部目的。除了小代码片段之外,您不应该使用它。 (我也不在那里使用它!:D)
第二个太冗长了。不实用。
我个人喜欢第三种风格,即输入完全限定的名称(例如 std::cout)。
请记住,代码被读取的次数远多于编写和写入的次数。使用完全限定的名称 IMO 可以使您的代码更具可读性。
A very good explanation is given here.
The first style i.e. using namespace whatever defeats the whole purpose of namespacing. You should never be using it except in small code snippets. (I don't use it there either! :D )
Second one is way too verbose. Not practical.
I personally like the third style i.e. typing out the fully qualified name (e.g. std::cout).
Remember, the code is read much more times than it is written & using fully qualified names IMO makes your code more readable.
这个问题以前已经被问过很多次了,但我的SO搜索功能似乎暂时抛弃了我。基本上:
永远不要在头文件中放置任何类型的 using 指令 - 这样做会污染您的代码并引入各种难以追踪的错误。
在大量使用此类类型的实现 (.cpp) 文件中首选 using 声明,例如
using std::string
。作为最后的手段,使用
using namespace std
,但仅在实现文件中 - 为了方便起见,我在 SO 上的可编译代码的帖子中使用它。This has been asked many times before, but my SO search fu seems to have deserted me for the moment. Basically:
Never put a using directive of any sort in a header file – doing so will contaminate your code and introduce all sorts of hard to track down bugs.
Prefer a using declaration, like
using std::string
, in an implementation (.cpp) file that makes heavy use of such a type.As a last resort, use
using namespace std
, but only in implementation files – I use it in posts of compilable code on SO, for convenience.这是我就同一主题撰写的另一个答案的修改版本。这些问题已经够多了,也许我最终会发表一篇明确的文章;)
主要问题是名称冲突,因为如果您的代码中有一个名为 cout 的变量,并且您正在使用名称空间 std; 它你的意思会很模糊。它不仅仅是
cout
。count
、reverse
和equal
也将包括在内,这些都是常见的标识符。不考虑编译器的所有问题,对于任何来阅读您的代码的人来说这也是一个问题。这额外的 5 个字符可确保下一个人维护您的代码准确地了解您的意思。
还值得注意的是,您永远不应该放入
头文件,因为它可以传播到包含该头文件的所有文件,即使它们不想使用该名称空间。这里的另一个问题是,还不清楚 std 命名空间是否已导入,因此维护者(或 3 个月后的您)添加了一个与包含在同一编译单元中的一些晦涩的 std 函数同名的变量,并且然后花了一个小时试图找到编译错误的原因。
在大多数情况下,使用诸如
交换的专门版本是非常有益的,编译器将使用它,否则它将依赖于
std::swap
。如果你调用std::swap,你总是使用基本版本,它不会调用专用版本(即使它存在)。以使用 pimpl 惯用法 的代码为例。默认副本可以复制实际实现中的所有数据,而需要做的只是交换指针。使用专门的交换可以节省大量的执行时间,设计良好的库应该对其进行专门化。
总之,
总是更喜欢
使用 std::swap
而不是std::swap()
避免在标头中使用命名空间 std,因为传播会不惜一切代价,尽量避免在实现文件中使用它。< /p>
在每个文件的顶部都有数千个
using std::foo
并不是正确的方法。最多将它用于常用的类。其他一切都是意见。
This is a modified version of another answer I wrote on the same subject. Enough of these questions and maybe I will end up with a definitive post ;)
The major issue is name conflicts, in that if you have a variable called cout in your code and you are
using namespace std;
it will be ambiguous as to what you mean. It isn't justcout
.count
,reverse
andequal
will also be included, which are all common identifiers.Disregarding all problems to the compiler, it is also a problem for anyone coming to read your code. Those extra 5 characters ensure that the next person maintaining your code knowns exactly what you mean.
It is also worth noting that you should never put
In a header file, as it can propagate to all files that include that header file, even if they don't want to use that namespace. Another problem here is that it is also not clear that the std namespace has been imported, so the maintainer (or you in 3 months time) adds a variable with the same name as some obscure std function that was included into the same compilation unit and then spends an hour trying to find the cause of the compilation error.
In the majority of cases it is very beneficial to use things like
As if there is a specialized version of swap, the compiler will use that, otherwise it will fall back on
std::swap
. If you callstd::swap
, you always use the basic version, which will not call the specialized version (even if it exists).Take for example code using the pimpl idiom. Where as the default copy may copy all the data in the actual implementation, where as all that needs doing is swapping the pointers. Using a specialized swap could save huge amounts of execution time, and well designed libraries should specialize it.
In summary,
Always prefer
using std::swap
overstd::swap()
Avoid
using namespace std
in a header at all costs due to propagation, try to avoid using it in implementation files.Having thousands of
using std::foo
at the top of every file is not the way to go. At most use it for commonly use classes.Everything else is opinion.
我个人更喜欢第三种选择。看看这个:
然后将其用作:
相反,如果您可以说,
虽然可能需要更多时间来输入代码,但第二个和第三个选项更(有点)更受欢迎。
I personally prefer the third option. Just have a look at this:
and you use it as:
Instead if you could just say,
Although it might take a bit more time to type the code out, the second and the third options are more (kind of) preferred.
这是一个风格问题,但有一点是:您永远不应该将名称空间导入到全局范围中。例如:
如果您想导入名称空间,只需将其导入您正在工作的范围:
It is a matter of style, but for one thing: You should never import a namespace into the global scope. e.g.:
If you want to import the namespace just import it to the scope where you are working:
我只想指出
using namespace foo
是有范围的。示例:如果小心使用,
using namespace std
既有用又无害。I'd just like to point out that
using namespace foo
is scoped. Example:When used with care,
using namespace std
can be useful and harmless at the same time.我总是列出完整的命名空间。它提高了可读性,并让其他人知道您的函数和数据来自哪里。在阅读其他人的代码时,使用
using
非常烦人,尤其是当我试图学习某些东西时,因为我无法判断它是该名称空间的一部分还是另一个名称空间的一部分。就像其他明智的人所说的那样,它确实违背了命名空间的全部意义,不是吗?关键是将所有内容分开并赋予数据和逻辑意义。记住一个好顺序:最重要的人是使用该程序的客户;第二重要的是其他正在维护或试图从您的代码中学习的编码人员;最不重要的是你。 :-)
I always list out the full namespace. It improves readability and lets other people know where your functions and data are coming from.
using
is very annoying when reading other peoples' code, especially when I'm trying to learn something, because I can't tell if it's part of that namespace or the other. And like these other sensible people are saying, it does defeat the whole point of namespacing, doesn't it? The point is to keep everything separate and give meaning to the data and logic.A good order to remember: the most important person is the client who uses the program; second most important is other coders who are either maintaining or trying to learn from your code; least important is you. :-)
当命名空间为 std 时,建议写出完全限定名称是一回事,其中 std:: 仅添加 5 个额外字符。当命名空间堆叠时,这是一个完全不同的问题,并且您面临着这样的编写:
特别是如果您有公司风格的指导线将行数限制为 80 个字符,并且您添加了更多缩进。类似的东西掩盖了所有措辞背后的代码逻辑。从那时起,您开始欣赏使用和/或本地名称空间别名,以提高可读性。
It's one thing to recommend writing out the fully qualified name when the namespace is std, where std:: only adds 5 extra characters. It's a whole 'nother issue when namespaces stack, and you are faced with writing something like:
especially if you have a company style guidline limiting lines to 80 characters and you add a few more indents. Something like that obscures the logic of the code behind all the verbiage. At that point, you start to appreciate using and/or local namespace aliases, in the interest of readability.
无论你喜欢什么。真的,没关系。
然而,大多数代码片段使用
Whatever you prefer. Really, it doesn't matter.
However, most code-snippets use