有没有办法让 C++ 预处理器使用连接符标记来连接参数?
我知道我能做到:
#include <boost/preprocessor/seq/cat.hpp>
#define arg1 foo
#define arg2 bar
#define arg3 baz
BOOST_PP_SEQ_CAT((arg1)(_)(arg2)(_)(arg3))
获取foo_bar_baz
。
我有两个问题:
- 没有重复的显式连接器有没有办法做到这一点
字符 (
(_)
) 和可变长度的参数列表? 是否有必要像这样传递参数:
(arg1)(arg2)(arg3)
我可以将它包装在另一个允许我正常传递参数的宏中吗?:
arg1, arg2, arg3
最佳答案
#define BOOST_PP_VARIADICS
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/fold_left.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>
#define OP(s, state, x) BOOST_PP_CAT(state, BOOST_PP_CAT(_, x))
#define COMPOSE(...) BOOST_PP_SEQ_FOLD_LEFT(OP, BOOST_PP_SEQ_HEAD(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)), BOOST_PP_SEQ_TAIL(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)))
#define arg1 foo
#define arg2 bar
#define arg3 baz
COMPOSE(arg1, arg2, arg3)
关于C++ 预处理器——连接参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31076419/