Why would I want to write something like this and fill // STUFF
with stuff?
// boost
namespace another_name {} namespace boost = another_name; namespace another_name {
// STUFF
}
This is just a regular namespace definition followed by a namespace alias definition aliasing the former.
// Namespace definition of namespace 'another_name'.
namespace another_name {}
// Namespace alias definition, 'boost' aliases 'another_name'.
namespace boost = another_name;
// Namespace (re-)definition: open up the declarative region
// described by the namespace 'another_name' to add e.g.
// function declarations, definitions and so on.
namespace another_name {
void foo() {}
}
// foo() is available via the namespace alias 'boost'.
int main() { boost::foo(); }
Note however that defining an alias name boost
could conflict with an actual namespace boost
namespace alias_name = ...
...
alias_name
must be a name not previously used ...
such that as soon as you actually include a boost header you will run into a compiler error:
// Existing namespace(s)
namespace boost {}
namespace another_name {}
// Conflicing alias_name.
namespace boost = another_name; // error: redefinition of 'boost
If you've seen this particular example in a code base, it could be an attempt at providing an internal what seems-to-be boost (subset) implementation of boost that could later be replaced by removing the alias and including the actual boost headers, but without having to change call sites that explicitly qualifies entities using boost::
.
E.g., an early version of the code base (before using actual boost):
// internal_boost_like_impl.h
namespace internal_boost_like_impl {
// Until-boost implementation of boost::mp11::mp_identity.
namespace mp11 {
template<typename T>
struct mp_identity {
using type = T;
};
template<typename T>
using mp_identity_t = typename mp_identity<T>::type;
}
}
// internal_boost.h
#include "internal_boost_like_impl.h"
namespace boost = internal_boost_like_impl;
// Code base looks to be using boost, but actually
// uses implementations from internal_boost_like_impl.
#include "internal_boost.h"
template<typename T>
struct Foo { T t; };
template<typename T>
void addToFoo(Foo<T>& foo,
boost::mp11::mp_identity_t<T> val) { foo.t += val; }
// ^^^^^^^^^^^^^ T in non-deduced context.
where later internal_boost.h
is modified into
// internal_boost.h
#include "boost/mpl/identity.hpp"
This approach could arguably facilitate variants easier (say one product variant where 3rd party libs may not be used due to e.g. safety critical concerns) but it may also confuse developers, and an arguably better approach would be to provide a unique alias that is simply set to different values for different variants/versions (rather than providing an alias that is named as and later entirely replaced by an actual well-known namespace such as boost
).