Boost logo

Boost :

From: Ed Brey (brey_at_[hidden])
Date: 2002-07-09 14:36:41


"Aleksey Gurtovoy" <agurtovoy_at_[hidden]> wrote:
> FWIW, MPL provides a special header, "boost/mpl/alias.hpp", that user can
> include to get an equivalent of
>
> namespace {
> namespace mpl = boost::mpl;
> }
>
> definition, e.g.
>
> #include "boost/mpl/list.hpp" // for boost::mpl::list
> #include "boost/mpl/alias.hpp" // namespace alias
>
> // now we can use 'mpl::' instead of longish 'boost::mpl::'
> typedef mpl::list<char,short,int> types;

This technique is prone to clashes that a namespace alias at global scope is. For example, suppose I make a (non-boost) library and provide trimmed-down version of it called "My Program Light". Suppose I wrap it in namespace brey::mpl. Now suppose you create a program with two different classes. In c1.hpp you use and include boost::mpl and use boost/mpl/alias.hpp to create an alias, and in c2.hpp you use and include brey::mpl and use brey/mpl/alias.hpp to create an alias. Now suppose you have a third class, c3, which needs to include both c1.hpp and c2.hpp. This demonstrates how having an alias.hpp file doesn't really solve anything in the general case, but rather can only help mask the problem. The one case where alias.hpp could be useful it if it is only included from a .cpp file, rather than a .hpp file; however, the potential for misuse it too high to warrant a header file to save two lines of code.

My experience has been that the way to robustly solve the alias problem is to centralize the definition of aliases. Each namespace has its own center. So if you have a component that lives in the namespace MyComponent, you have a single set of header files that include and alias various resources. For example, if several files in MyComponent use mpl, you would create a header called mpl.hpp, which would look like this:

// MyComponent/mpl.hpp
#include <boost/mpl/list.hpp>
namespace MyComponent {
  namespace boost::mpl;
  typedef mpl::list<char,short,int> types;
}

You have now reserved the names "mpl" and "types" for your namespace. Any code that needs to use these names needs to include mpl.hpp. Obviously, you could bundle as many or as few names into a given centralized header file as you like.

My recommendation would be to not include an alias file in the ublas library and to remove the alias file from the mpl library.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk