#include
,我立即想到这是不可能的。...只有后来才发现它确实是可能的,你只需要注意参数扩展(例如Boost.Preprocessor可以处理)。
虽然如果可能的话,我会避免真正使用这种方式进行包含,但我想知道为什么它会起作用。目前,我无法从C++或C标准中获得有用的理解。
任何预处理指令都允许使用带参数的宏吗?(除了
#define
/#undef
)能否引用并总结一下允许使用这种方法的地方?
以下是一个使用Boost.Preprocessor简单演示的例子,供好奇者参考:
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>
#define INC_LOCAL(a,b) BOOST_PP_STRINGIZE(BOOST_PP_CAT(BOOST_PP_CAT(a,b),.h))
#define INC_GLOBAL(a,b) BOOST_PP_CAT(BOOST_PP_CAT(<,a),BOOST_PP_CAT(b,>))
#include INC_LOCAL(loc,al) // #include "local.h"
#include INC_GLOBAL(vect,or) // #include <vector>
更新:参考了C语言标准,澄清问题。
pp-tokens
能让我到达preprocessing-token
,但只有preprocessing-op-or-punc
看起来不错,但只给出了一些操作符。 - Georg Fritzsche