1. 问题描述
因为一下库定义了宏and,导致boost的文件包含and.hpp展开成&.hpp,所以出现以下错误
Cannot open include file: ‘boost/mpl/aux_/preprocessed/plain/&.hpp’
2. 解决方案
-
在定义宏之前包含boost文件,但这种方案的缺点是:当文件包含很复杂时,这样的位置不好找
-
使用#pragma push_macro(“and”) #pragma pop_macro(“and”),在包含头文件时,先保存and的宏,然后undef,最后恢复and宏,但需注意编译器兼容性,MSVC 和 GCC/Clang 支持的这项扩展指令。示例如下:
#ifdef and #pragma push_macro("and") //保存and宏定义 #undef and #include BOOST_PP_STRINGIZE(boost/and.hpp) //包含头文件 #pragma pop_macro("and") //恢复and宏定义 #else #include BOOST_PP_STRINGIZE(boost/and.hpp) #endif