How to check for TR1 while compiling?

牧云@^-^@ 提交于 2019-12-05 11:05:31

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

GCC-4.3 has:

#define __GXX_EXPERIMENTAL_CXX0X__ 1

But, this is obviously not standard.

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.

One library I deal with needs to use some classes that got added to TR1 from Boost, preferring TR1 if available. The solution (being a Unix-based library) is to shove the checks into the configure script.

So in other words, no, nothing portable that I know of. That said, if you're on Unix, the configure script checks work well enough.

Assuming one is using VS2010, or any suite that has TR1 available, what would happen if one were to do

#include "boost/tr1/unordered_map.hpp"
...
std::tr1::unordered_map< ... > uMap;

What would be the type of uMap be?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!