[英]Namespaces qualified with :: in C++
What does it mean if namespace in C++ is qualified with ::
? 如果C ++中的命名空间符合
::
?这意味着什么? For example ::testing::Test
. 例如
::testing::Test
。
::
is the scope resolution operator. ::
是范围解析运算符。 It always means "search the global namespace for the symbol on the right." 它总是意味着“在全局命名空间中搜索右边的符号”。 For example:
例如:
namespace testing {
int a = 1;
}
namespace foo {
namespace testing {
int a = 2;
}
int b = ::testing::a; // b has the value 1
int c = testing::a; // c has the value 2
}
It means that the testing
namespace being referred to is the one off the global namespace, rather than another nested namespace named testing
. 这意味着所引用的
testing
命名空间是全局命名空间之外的命名空间,而不是另一个名为testing
嵌套命名空间。
Consider the following corner case, and probably an example of bad design: 考虑以下极端情况,可能是糟糕设计的一个例子:
namespace foo
{
struct gizmo{int n_;};
namespace bar
{
namespace foo
{
float f_;
};
};
};
int main()
{
using namespace foo::bar;
::foo::gizmo g;
g.n_;
}
There are 2 namespaces named foo
. 有两个名为
foo
名称空间。 One is the top-level hanging off of the "global" namespace, and another one is nested within foo::bar
. 一个是“全局”命名空间的顶级挂起,另一个嵌套在
foo::bar
。 then we go on to using namespace foo::bar
, meaning any unqualified reference to gizmo
will pick up the one in foo::bar::foo
. 然后我们继续
using namespace foo::bar
,这意味着对gizmo
任何非限定引用都会在foo::bar::foo
。 If we actually want the one in foo
then we can use an explicit qualification to do so: 如果我们真的想要
foo
那个,那么我们可以使用一个明确的资格来这样做:
::foo::gizmo
If ::
is used on the left side it means the global scope. 如果
::
在左侧使用,则表示全局范围。
If you want an analogy with the filesystem, testing::Test
would be a kind of relative path (with respect of König lookup ) whereas ::testing::Test
would be an absolute path. 如果你想要与文件系统类比,那么
testing::Test
将是一种相对路径(关于König查找 )而::testing::Test
将是一个绝对路径。 I hope this analogy makes it clearer and doesn't mess up your mind :-). 我希望这个类比能让它更清晰,不会弄乱你的想法:-)。
I if you precede a variable name with ::, it resolves the variable to the global scope. 如果您在变量名前面加上::,它会将变量解析为全局范围。 In this way, you can have both a local variable testing and global variable testing and differentiate between them.
通过这种方式,您可以同时进行局部变量测试和全局变量测试,并区分它们。
#include <iostream>
using namespace std;
int testing = 37;
int main()
{
int testing = 42;
cout << ::testing << ' ' << testing << endl;
}
Output will be 37 42 输出将是37 42
This invokes something called the qualified name lookup:
这会调用称为
qualified name lookup:
$3.4.3/1 - "The name of a class or namespace member can be referred to after the :: scope resolution operator (5.1) applied to a nested-name-specifier that nominates its class or namespace. During the lookup for a name preceding the :: scope resolution operator, object, function, and enumerator names are ignored. If the name found is not a class-name (clause 9) or namespace-name (7.3.1), the program is ill-formed." $ 3.4.3 / 1 - “可以在:: scope resolution运算符(5.1)应用于指定其类或名称空间的嵌套名称说明符之后引用类或名称空间成员的名称。在查找名称期间在:: scope解析运算符之前,对象,函数和枚举器名称被忽略。如果找到的名称不是类名(第9句)或名称空间名称(7.3.1),则程序格式错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.