1

I have a namespace hierarchy and want to give an abbreviations for some long namespace names. For example, I have

Math::Geometry::OneDimension::

and I want to use Ge for Geometry and D1 for OneDimension thus the following works

Math::Ge::OneDimension::
Math::Geoemtry::D1::
Math::Ge::D1::

Is it possible to use namespace alias to do it?

user1899020
  • 12,499
  • 17
  • 70
  • 145

4 Answers4

8

You can use namespace aliases:

namespace D1 = Math::Geometry::OneDimension;
Luchian Grigore
  • 245,575
  • 61
  • 446
  • 609
3

To access it like that, you'll need to declare namespace aliases inside their enclosing namespaces:

namespace Math {
   namespace Ge = Geometry;
   namespace Geometry {
       namespace D1 = OneDimension;
   }
}

You can, of course, declare the aliases in other scopes, and access them simply as Ge and D1 in that scope.

Mike Seymour
  • 242,813
  • 27
  • 432
  • 630
2
namespace Ge = Math::Geonetry::OneDimension;
John Dibling
  • 97,027
  • 28
  • 181
  • 313
1

Either you can do the aliasing inside the namespace, or, you can do this, from the outside of the namespace :

namespace Ge = Math::Geometry;
namespace D1 = Ge::OneDimension;

Ge::element_of_geometry;
D1::element_of_one_dimension;

I prefer this solution but use it in a scope to avoid name clashing.

Johan
  • 3,688
  • 15
  • 25