I am currently writing a C++ library which will be required to compile with both GCC for linux and Sun CC for Solaris. In the interest of performance I am creating some classes which select different headers based on compiler; GCC with c++0x or TR1 or with niether and Sun CC RogueWave or STLPort. I’m sturggling to work out the best means of #ifdef’ing the typedefs, for example:
namespace project {
#if defined(__GNUG__)
#if defined(HAVE_CXXOX)
#include <unorderd_map>
typedef srd::unordered_map map;
#elif defined(HAVE_TR1)
#include <tr1/unordered_map>
typedef std::tr1::unordered_map map;
#else
#include <map>
typedef std::map map;
#endif
#elif defined(__SUNPROC_CC)
#include <map>
typedef std::map map;
#endif
} //namespaces
This won’t work for two reasons:
namespace project { ... }. (If the header contains nothing but templates and inline functions, it might work anyway, but I wouldn’t count on it.)typedefdoesn’t work on templates. There’s a workaround where you define an empty derived class.So perhaps something like this: