Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for TR1 while compiling?

We are programming a logging library that keeps itself in a .hpp file. We would like to include <tr1/unordered_map> (if the compiler supports TR1,) or the standard <map> otherwise. Is there a standard way of checking at compile time if tr1 is available or not?

I was thinking that the same way that the "__cplusplus" define symbol is present, there could have been defined a "__cxx__tr1" or something like that. I haven't seen that in the drafts for TR1, so I assume it is not present, but I wanted to ask first just in case.

As a note, if those defines don't exist, it wouldn't be a bad idea to include them in proposals themselves.

like image 424
Diego Sevilla Avatar asked Apr 07 '09 07:04

Diego Sevilla


3 Answers

If you are using any configuration tools like autotools you may try to write a test like:

AC_CHECK_HEADER(tr1/unordered_map,[AC_DEFINE([HAVE_TR1],[],["Have tr1"])],[])
AC_CHECK_HEADER(unordered_map,[AC_DEFINE([HAVE_CXX0X],[],["Have C++0x"])],[])

And then use these defines in your code.

Generally speaking __cplusplus macro should give you standard version number, but there is no compiler that gives you 100% standard implementation... Thus write configure macros.

Unfortunately this is only quite reliable way to check such things unless you want to write 1001 #ifdef for each compiler (what boost does)

And then:

#include "config.h"
#ifdef  HAVE_CXX0X
#  include <unordered_map>
   typedef std::unordered_map<foo,bar> my_map;
#elif HAVE_TR1
#  include <tr1/unordered_map>
   typedef std::tr1::unordered_map<foo,bar> my_map;
#else
#  include <map>
   typedef std::map<foo,bar> my_map;
#endif
like image 98
Artyom Avatar answered Oct 12 '22 12:10

Artyom


GCC-4.3 has:

#define __GXX_EXPERIMENTAL_CXX0X__ 1

But, this is obviously not standard.

like image 27
greyfade Avatar answered Oct 12 '22 12:10

greyfade


See ISO C++ (WG21) paper N1575. This paper has been dropped from TR1, with no replacement. So there is no official way to detect TR1.

like image 33
MSalters Avatar answered Oct 12 '22 13:10

MSalters