Don't use the{{using namespace ...}} or using ... constructs in a header file. It might save you some typing but it also forces the namespaces you use onto every .cpp file that directly or indirectly includes your headers. That could create name clashes with names in some other namespace that the author of the .cpp file wants to use. Use fully qualified names, painful as it might be.

There is one exception. It's sometimes handy to "import" names from one namespace to another. For example suppose some C++ compiler doesn't provide the std::tr1::auto_ptr template, which is used in qpid. Boost does provide a compatible boost::auto_ptr but it's in the boost namespace and qpid expects it in std::tr1. No problem, we create our own tr1/memory header file:

#include <boost/memory>
namespace std { 
  namespace tr1 {
    using boost::auto_ptr;
  }
}

This makes the boost template available in the standard namespace. (Actually you don't need to do this yourself, boost provides a set of adapter headers for all the tr1 stuff.)

  • No labels