C++ 在函数实现中使用命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4811596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using namespace in function implementation
提问by Sean
In C++, could I use using namespace std;
declaration in the function implementation files?
在 C++ 中,我可以using namespace std;
在函数实现文件中使用声明吗?
采纳答案by mgiuca
By "function implementation files" do you mean the .h files or the .cpp files? (I would normally call .cpp files "implementation" files, while .h files are "interface" files.)
“函数实现文件”是指 .h 文件还是 .cpp 文件?(我通常将 .cpp 文件称为“实现”文件,而 .h 文件是“接口”文件。)
If you mean, .cpp files, then of course. That is where you normally see using namespace std
. It means that all the code in this .cpp file has access to std
without qualification.
如果您的意思是 .cpp 文件,那么当然。那是你通常看到的地方using namespace std
。这意味着这个 .cpp 文件中的所有代码都可以std
无条件访问。
If you mean .h files, then you can, but you shouldn't. If you include it in a .h file, it will automatically apply to any .cpp file which includes the .h file, which could be a lot of files. You generally don't want to be telling othermodules which namespaces to import. It is best to put it in every .cpp file rather than in a common .h file.
如果你的意思是 .h 文件,那么你可以,但你不应该。如果您将它包含在 .h 文件中,它将自动应用于任何包含 .h 文件的 .cpp 文件,这可能是很多文件。您通常不想告诉其他模块要导入哪些名称空间。最好将它放在每个 .cpp 文件中,而不是放在普通的 .h 文件中。
Edit: User @lachy suggested an edit which I won't include verbatim, but they suggested I point out that using namespace std
is usually considered bad practice, due to namespace pollution. They gave a link to a question on this topic: Why is "using namespace std;" considered bad practice?
编辑:用户@lachy 建议了一个编辑,我不会逐字包括在内,但他们建议我指出using namespace std
,由于命名空间污染,这通常被认为是不好的做法。他们提供了一个关于这个主题的问题的链接:Why is "using namespace std;" 被认为是不好的做法?
回答by Mikael Persson
Maybe you would like to know too that you can put using namespace std;
within a function body as well, like below. This will restrict the scope of the using namespace
statement.
也许你也想知道你也可以放在using namespace std;
函数体中,如下所示。这将限制using namespace
语句的范围。
void f() {
using namespace std;
cout << "Foo" << endl;
//..
};
void g() {
cout << "Bar" << endl; //ERROR: cout and endl are not declared in this scope.
};
This can be useful if you want to use a lot of elements of a namespace in the body of a function that is written in a header file (which you shouldn't per se, but sometimes it is OK or even almost necessary (e.g. templates)).
如果您想在编写在头文件中的函数体中使用命名空间的许多元素(您本身不应该这样做,但有时它是可以的,甚至几乎是必需的(例如模板),这可能很有用))。
回答by Maxpm
I'm assuming you mean something like this:
我假设你的意思是这样的:
// Foo.h
void SayHello();
...
...
// Foo.cpp
#include "Foo.h"
using namespace std;
void SayHello()
{
cout << "Hello, world!" << endl;
}
If that is the case, then yes. However, it's considered bad practice to use using namespace std;
in larger projects.
如果是这样,那么是的。但是,using namespace std;
在大型项目中使用被认为是不好的做法。
回答by Nim
if by "function implementation files" you mean the .C/.cpp etc. files, you can, however try to avoid. Instead inject what you need in only, for example if you need <iostream>
for std::cout
, std::endl
etc. just inject these two in, using std::cout;
and using std::endl;
, now you can simply write cout
and endl
.
如果“函数实现文件”是指 .C/.cpp 等文件,则可以尝试避免。相反,注入你所需要的只有,例如,如果你需要<iostream>
的std::cout
,std::endl
等刚注入这两个在,using std::cout;
并且using std::endl;
,现在你可以简单的写cout
和endl
。