Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace alias scoping issues

Tags:

c++

namespaces

I have a header file in which I wish to use a namespace alias while defining a class. However I don't want to expose this alias to anything that includes the header file.

// 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::
};

How can I alias a namespace for a class declaration without it leaking out everywhere?

like image 958
wxffles Avatar asked Jul 06 '11 21:07

wxffles


People also ask

What is a namespace alias?

Namespace aliases allow the programmer to define an alternate name for a namespace. They are commonly used as a convenient shortcut for long or deeply-nested namespaces.

Can we create alias of a namespace?

You can also create an alias for a namespace or a type with a using alias directive.


1 Answers

namespace MyClassSpace
{
namespace qux = boost::std::bar::baz::qux;

class foo
{
  // use qux::
};

}

using MyClassSpace::foo; // lift 'foo' into the enclosing namespace

This is also how most Boost libraries do it, put all their stuff in a seperate namespace and lift the important identifiers into the boost namespace.

like image 77
Xeo Avatar answered Oct 04 '22 09:10

Xeo