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
  • 13,167
  • 21
  • 79
  • 154

4 Answers4

8

You can use namespace aliases:

namespace D1 = Math::Geometry::OneDimension;
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
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
  • 249,747
  • 28
  • 448
  • 644
2
namespace Ge = Math::Geonetry::OneDimension;
John Dibling
  • 99,718
  • 31
  • 186
  • 324
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,728
  • 16
  • 25