C++ 中“使用”声明的范围是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/223021/
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
What's the scope of the "using" declaration in C++?
提问by Jeff Lake
I'm using the 'using' declaration in C++ to add std::string and std::vector to the local namespace (to save typing unnecessary 'std::'s).
我在 C++ 中使用“using”声明将 std::string 和 std::vector 添加到本地命名空间(以节省输入不必要的“std::”)。
using std::string;
using std::vector;
class Foo { /*...*/ };
What is the scope on this declaration? If I do this in a header, will it inject these 'using' declarations into every cpp file that includes the header?
本声明的范围是什么?如果我在标头中执行此操作,是否会将这些“使用”声明注入包含标头的每个 cpp 文件中?
采纳答案by Jeremy Ruten
When you #include a header file in C++, it places the whole contents of the header file into the spot that you included it in the source file. So including a file that has a using
declaration has the exact same effect of placing the using
declaration at the top of each file that includes that header file.
当您在 C++ 中 #include 头文件时,它会将头文件的全部内容放入您在源文件中包含它的位置。因此,包含具有using
声明的文件与将using
声明置于包含该头文件的每个文件的顶部具有完全相同的效果。
回答by Eclipse
There's nothing special about header files that would keep the using
declaration out. It's a simple text substitution before the compilation even starts.
头文件没有什么特别的,可以将using
声明排除在外。在编译开始之前,这是一个简单的文本替换。
You can limit a using
declaration to a scope:
您可以将using
声明限制为一个范围:
void myFunction()
{
using namespace std; // only applies to the function's scope
vector<int> myVector;
}
回答by dagorym
The scope of the using statement depends on where it is located in the code:
using 语句的范围取决于它在代码中的位置:
- Placed at the top of a file, it has scope throughout that file.
- If this is a header file, it will have scope in all files that include that header. In general, this is "not a good idea" as it can have unexpected side effects
- Otherwise the usingstatement has scope within the block that contains it from the point it occurs to the end of the block. If it is placed within a method, it will have scope within that method. If it is placed within a class definition it will have scope within that class.
- 放置在文件的顶部,它具有整个文件的范围。
- 如果这是一个头文件,它将在包含该头文件的所有文件中具有作用域。一般来说,这“不是一个好主意”,因为它可能会产生意想不到的副作用
- 否则using语句在包含它的块内具有范围,从它出现的点到块的结尾。如果它被放置在一个方法中,它将在该方法中具有作用域。如果它被放置在一个类定义中,它将在该类中具有作用域。
回答by JohnMcG
The scope is whatever scope the using declaration is in.
作用域是 using 声明所在的任何作用域。
If this is global scope, then it will be at global scope. If it is in global scope of a header file, then it will be in the global scope of every source file that includes the header.
如果这是全局范围,那么它将在全局范围内。如果它在头文件的全局范围内,那么它将在包含头文件的每个源文件的全局范围内。
So, the general advice is to avoid using declarations in global scope of header files.
因此,一般建议是避免在头文件的全局范围内使用声明。
回答by James Curran
In the case cited, the file ("translation unit"), which means yes, every file that includes it.
在引用的案例中,文件(“翻译单元”),这意味着是,包含它的每个文件。
You can also put the using statement inside the class, in which case, it's in effect just for that class.
您也可以将 using 语句放在类中,在这种情况下,它只对那个类有效。
Generally, if you need to specify a namespace in a header, it's often best to just fully-qualify every identifier necessary.
通常,如果您需要在标头中指定命名空间,通常最好只对每个必要的标识符进行完全限定。
回答by Ates Goral
That is correct. The scope is the module that uses the using
declaration. If any header files that a module includes have using
declarations, the scope of those declarations will be that module, as well as any other modules that include the same headers.
那是正确的。作用域是使用using
声明的模块。如果模块包含的任何头文件具有using
声明,则这些声明的范围将是该模块,以及包含相同头文件的任何其他模块。
回答by MSalters
There are a few comments that are rather unqualified when they say "Don't". That is too stern, but you have to understand when it is OK.
当他们说“不要”时,有一些评论是相当不合格的。这太严厉了,但你必须明白什么时候可以。
Writing using std::string
is never OK. Writing using ImplementationDetail::Foo
in your own header, when that header declares ImplementationDetail::Foo can be OK, moreso if the using declaration happens in your namespace. E.g.
写作using std::string
从来都不是好事。using ImplementationDetail::Foo
在您自己的头文件中写入,当该头文件声明 ImplementationDetail::Foo 时可以,如果 using 声明发生在您的命名空间中。例如
namespace MyNS {
namespace ImplementationDetail {
int Foo;
}
using ImplementationDetail::Foo;
}