Zac*_*Zac 5 c++ boost tr1 c-preprocessor c++11
我正在尝试编写一些可移植的C++库代码,这些代码最初将依赖于Boost.Regex,然后在编译器支持时转移到TR1,并在从std :: tr1名称空间移动后最终转到C++ 0x规范到标准 这是我想要对预处理器做什么的一些伪代码:
if( exists(regex) ) // check if I can #include <regex>
{
#include <regex> // per TR1
if( is_namespace(std::tr1) ) // are we on TR1 or C++0x?
{
using std::tr1::regex;
} else
{
using std::regex;
}
} else // fall-back to boost
{
#include <boost/regex.hpp>
using boost::regex;
}
Run Code Online (Sandbox Code Playgroud)
当然,所有这些都需要在预处理器指令中,但如果我知道如何实现,我不会在这里问.:)
Meh*_*ari 10
如果不在预处理之前依赖第三方的东西,就无法做到这一点.通常,autoconf可以使用类似的东西来实现这一目标.
它们的工作原理是生成另一个头文件,其中包含#define指示您要使用的头/库存在的指令.
小智 6
你不能这样做.C++的预处理阶段发生在编译阶段之前,因此C++语言条件结构无法有效地放在#include等预处理程序指令周围.
如果需要条件编译,那么使用#ifdef的方法就是这样.就像是:
#ifdef BOOST_BUILD
#include "somebooststuff.h"
using namespace boost;
#else
#include "somestdstuff.h"
using namespace std;
#endif
Run Code Online (Sandbox Code Playgroud)
其中BOOST_BUILD是您在makefile中定义的预处理程序符号或其他任何内容.
| 归档时间: |
|
| 查看次数: |
1680 次 |
| 最近记录: |