面对在预处理器中是否可以选择#include
s的问题,我立即认为不可能。
..后来才发现它确实可行,你只需要注意参数扩展(例如Boost.Preprocessor可以处理)。宏作为预处理器指令的参数
尽管如果可能的话,我会避免实际做到这一点,但我想知道为什么这会起作用。目前,我未能在C++或C标准中得到有用的理解。
参数化宏是否允许用于任何预处理器指令? (除了#define
/#undef
)
有人可以参考这是允许和总结吗?
实施例:
#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>
更新: 引用Ç标准,澄清问题。
好吧,'pp-tokens'让我进入'preprocessing-token',从那里只有'preprocessing-op-or-punc'看起来不错,但只给一些操作员。 – 2009-11-15 05:50:46
所以'像普通文本一样处理'意味着像'identifier(identifier,...)'这样的宏扩展。 – 2009-11-15 06:17:00
当然。结果可能有未定义的行为。 – outis 2009-11-15 06:31:43