C++当命名空间、类名和命名空间::类名冲突时该怎么办
C++ what do do when namespace, classname and namespace::classname clash
我继承了一些名字不好的代码,当我收到一个第三方库时,我很幸运,这使我的生活更加复杂。 这就是我最终得到的。
class Something; // third party library
namespace Something {
class Something;
class Templated<class TemplateClass>;
}
现在我需要使用第三方库中的类"Something"作为命名空间 Something 下新类的 TemplateClass 参数。 我认为这应该有效
class Something; // third party library
namespace Something {
class Something;
class Templated<class TemplateClass>;
class Impl : public Templated< ::Something > {}
}
但是编译器不喜欢它。 我让它编译的唯一方法是
class Something; // third party library
class Something2 : public Something {} // dirty hack
namespace Something {
class Something;
class Templated<class TemplateClass>;
class Impl : public Templated< Something2 > {}
}
但我真的不喜欢它。 必须有更好的方法来做到这一点。
您可以使用另一个命名空间:
class Something; // third party library
namespace third_party{
using ::Something;
}
namespace Something {
class Something;
class Templated<class TemplateClass>;
class Impl : public Templated< ::third_party::Something > {}
}
不过,总的来说,我认为将类和命名空间命名完全相同是一个非常糟糕的主意。
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- CUDA内核和数学函数的显式命名空间
- 嵌套的匿名命名空间
- CMakeLists.txt中的命名空间表示法
- 类是C++中的命名空间吗
- 在命名空间中使用全局命名空间中的函数
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- '使用命名空间{嵌套在另一个命名空间中的某个命名空间}"
- 是否可以将函数导入命名空间,但不能导出它?
- C++ C++类中的命名空间降级
- 如何使用 soong 命名空间来有条件地编译模块
- 使用 Clang++ 有没有办法将文件作为命名空间等包含?
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 如何通过命名空间调用非静态方法
- C++当命名空间、类名和命名空间::类名冲突时该怎么办