c++ - (c++) 可以从外部来源嵌套命名空间

标签 c++ nested namespaces

我有一些带有命名空间的标题,它们都遵循特定的名称模式
现在他们都在他们的实际名字前面有一个前缀,比如

namespace Xname{//inside name.h
  //stuff here
};
namespace Xsomething{//inside something.h
  //stuff here
};
现在这对我的使用 atm 非常有用,但我的想法是再创建一个包含一个命名空间的 header ,该命名空间将收集所有其他命名空间,以便我可以像这样访问它们:
#include "mainheader.h"
X::name::stuff
X::something::stuff
这样我就可以在将来像这样更改新标题的命名空间名称
X::name::stuff
hello::name::stuff
不能像这样嵌套它们:
namespace x{
   namespace something{
     //stuff
   }
}

最佳答案

您可以在彼此内部定义新的命名空间 like so

#include <iostream>

namespace Xname{//inside name.h
  const int x = 0;
};
namespace Xsomething{//inside something.h
  const int y = 1;
};

// Your other header
namespace X {
namespace name = Xname;
namespace something = Xsomething;
}

int main() {
  std::cout << X::name::x << "\n";
  std::cout << X::something::y << "\n";
}

关于c++ - (c++) 可以从外部来源嵌套命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67074429/

相关文章:

python - 从嵌套字典中动态删除项目

c++ - 我的 std::exchange 不在命名空间 std 中?

c++ - 未命名的命名空间与私有(private)变量

c++ - 你能搜索或过滤 Vim 补全吗?

c++ - Boost.Coroutine在 sleep 方面如何使用类似Unity3D Coroutine的方式?

c++ - 如何在 C++ 中连接两个字符串?

java - 为什么通常不允许嵌套 block 注释?

c++ - 嵌套类中的 "Invalid covariant return type"错误,其方法返回基于模板的对象

r - R 中的命名空间和泛型函数

python - 无法写入图像 SimpleElastix (Python)