c – 命名空间别名范围问题

前端之家收集整理的这篇文章主要介绍了c – 命名空间别名范围问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个头文件,我希望在定义一个类时使用命名空间别名.但是,我不想将此别名公开给包含头文件的任何内容.
  1. // foo.h
  2. namespace qux = boost::std::bar::baz::qux; // ! exposed to the world
  3. class foo
  4. {
  5. // can't put a namespace alias here
  6.  
  7. // stuff using qux::
  8. };

如何在没有泄漏到任何地方的情况下为类声明命名空间?

解决方法

  1. namespace MyClassSpace
  2. {
  3. namespace qux = boost::std::bar::baz::qux;
  4.  
  5. class foo
  6. {
  7. // use qux::
  8. };
  9.  
  10. }
  11.  
  12. using MyClassSpace::foo; // lift 'foo' into the enclosing namespace

这也是大多数Boost库如何做到的,将所有内容放在一个单独的命名空间中,并将重要的标识符提升到boost命名空间中.

猜你在找的C&C++相关文章