我有一个头文件,我希望在定义一个类时使用命名空间别名.但是,我不想将此别名公开给包含头文件的任何内容.
- // foo.h
- namespace qux = boost::std::bar::baz::qux; // ! exposed to the world
- class foo
- {
- // can't put a namespace alias here
- // stuff using qux::
- };
如何在没有泄漏到任何地方的情况下为类声明命名空间?
解决方法
- namespace MyClassSpace
- {
- namespace qux = boost::std::bar::baz::qux;
- class foo
- {
- // use qux::
- };
- }
- using MyClassSpace::foo; // lift 'foo' into the enclosing namespace
这也是大多数Boost库如何做到的,将所有内容放在一个单独的命名空间中,并将重要的标识符提升到boost命名空间中.