C++ 预处理器——连接参数
C++ preprocessor--join arguments
有没有办法让 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
怎么样:
#include <iostream>
#define COMPOSE(prefix,name) prefix##_##name
int main() {
int COMPOSE(first,par);
first_par = 1;
return first_par;
}
#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++ 预处理器使用连接器标记连接参数?
我知道我可以做到:
#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
怎么样:
#include <iostream>
#define COMPOSE(prefix,name) prefix##_##name
int main() {
int COMPOSE(first,par);
first_par = 1;
return first_par;
}
#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)