在命名空间中的类中,我可以只退出**类命名空间吗
Within a class in a namespace, can I exit **just** the class namespace?
此代码不编译:
class A;
void foo(A&) {
}
class A {
void foo() {
foo(*this); ///This does not compile
}
};
错误:
error: no matching function for call to 'A::foo(A&)'
foo(*this);
^
note: candidate is:
note: void A::foo()
这可以通过调用::foo(*this);
来解决
然而,让我们考虑一下我们在命名空间中的情况:
namespace bar {
class A;
void foo(A&) {
}
class A {
void foo() {
foo(*this); ///This does not compile
}
};
}
除了显式调用bar::foo(*this);
之外,还有其他方法吗?我的意思是,有没有办法在下一个周围的声明性区域中查找名称,即包含的bar
命名空间?
这个用例与这里看到的类似。
我的意思是,有没有办法在下一个周围的声明性区域中查找名称,即包含
bar
命名空间?
否。
你可以用另一种方式:
void foo() {
using bar::foo;
foo(*this); /// OK now
}
不在方法本身中。但是,您可以在.cpp文件中执行此操作:
namespace bar {
namespace {
auto outerFoo = foo;
}
void A::foo() {
outerFoo(*this);
}
}
请注意,名称outerFoo
是一个隐藏的实现细节,它不会导致名称冲突(因为它位于匿名命名空间中)。
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- CUDA内核和数学函数的显式命名空间
- 嵌套的匿名命名空间
- CMakeLists.txt中的命名空间表示法
- 类是C++中的命名空间吗
- 在命名空间中使用全局命名空间中的函数
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- '使用命名空间{嵌套在另一个命名空间中的某个命名空间}"
- 是否可以将函数导入命名空间,但不能导出它?
- C++ C++类中的命名空间降级
- 如何使用 soong 命名空间来有条件地编译模块
- 使用 Clang++ 有没有办法将文件作为命名空间等包含?
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 如何通过命名空间调用非静态方法
- 在命名空间中的类中,我可以只退出**类命名空间吗