使用 "::member" 引用全局命名空间有什么用吗?
Is there any use in using "::member" to reference the global namespace?
在C++中,命名空间myNamespace
的成员可以引用为myNamespace::member
。
但是,显然存在一种类似于上述语法的语法,用于引用全局命名空间 - 简单地::member
.考虑:
int foo() {
return 4;
}
::foo(); // What is the difference between
foo(); // these two lines?
简而言之,在这种情况下使用 foo();
和 ::foo();
之间是否存在功能差异,或者它们是否完全相同?
示例代码:
#include <iostream>
void foo() {
std::cout << 1;
}
namespace my_ns
{
void foo() {
std::cout << 2;
}
void goo1() {
::foo();
}
void goo2() {
foo();
}
}
int main(int c, char** args) {
my_ns::goo1();
my_ns::goo2();
return 0;
}
相关文章:
- C++ 未初始化的本地(非全局)int 数组中的元素类型到底是什么?
- 除了调用全局删除运算符之外,删除一个void指针还能做什么呢
- 从模块导出全局常量的正确方法是什么?
- 有什么陷阱用 constexpr string_view 替换全局 const char[]?
- 将静态全局变量声明为内联有什么意义吗?
- 有什么理由C++ 11+ std::mutex 应该声明为全局变量,而不是作为函数参数传递到 std::thread 中
- 类中的静态成员变量和C++中的全局变量有什么区别?
- 使用 "::member" 引用全局命名空间有什么用吗?
- C++中的大小释放:全局运算符delete的正确行为是什么(void*ptr,std::size_t size)
- 全局变量和内部类有什么区别?
- 静态成员功能和全局功能之间有什么不同
- 当托管它崩溃的可执行过程时,全局挂钩会发生什么
- 恒定声明全局或在使用它的函数中声明恒定声明之间有什么区别?
- C 我应该做什么而不是全局变量
- C 中静态全局和非静态全局标识符之间有什么区别?
- 标准全局默认运算符新的对齐限制是什么?
- 我什么时候可以确定一个 constexpr 全局变量会像 C 宏一样被"forgotten"?
- 动态链接共享库中的全局变量和静态变量会发生什么情况
- CUDA 中的全局内存和纹理有什么区别?
- C++:全局变量与局部变量.使用什么