问题描述
我们正在编程一个保存在.hpp文件中的日志记录库。我们希望包含< tr1 / unordered_map>
(如果编译器支持TR1,)或标准的< map>
否则。如果tr1有可用,是否有标准的检查方式?
我以为 __ cplusplus
定义符号存在,可能已经定义了一个 __ cxx__tr1
或类似的东西。我没有看到在TR1的草案中,所以我认为它不存在,但我想先问一下,以防万一。
作为一个注释,如果那些定义不存在,将它们包含在提案本身中并不是一个坏主意。
如果你是使用任何配置工具(如autotools),您可以尝试编写一个测试:
AC_CHECK_HEADER(tr1 / unordered_map,[AC_DEFINE([HAVE_TR1 ] [],[有tr1])],[])
AC_CHECK_HEADER(unordered_map,[AC_DEFINE([HAVE_CXX0X],[],[有C ++ 0x])],[]
然后在您的代码中使用这些定义。
一般来说,
__ cplusplus
宏应该给你标准的版本号,但是没有编译器给你100%的标准实现...所以写配置宏。 不幸的是,这只是非常可靠的方式来检查这样的事情,除非你想写1001 #ifdef
每个编译器(什么提升)
然后:
includeconfig.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
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.
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
这篇关于编译时如何检查TR1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!