我有一些带有命名空间的标题,它们都遵循特定的名称模式
现在他们都在他们的实际名字前面有一个前缀,比如
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/