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?
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.
You can also create an alias for a namespace or a type with a using alias directive.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With