C++预处理器—联接参数

C++ preprocessor--join arguments

本文关键字:参数 预处理 处理器 C++      更新时间:2023-10-16

有没有一种方法可以让C++预处理器使用joiner令牌连接参数?

我已经学会了我可以做:

#include <boost/preprocessor/seq/cat.hpp>
#define arg1 foo
#define arg2 bar
#define arg3 baz
BOOST_PP_SEQ_CAT((arg1)(_)(arg2)(_)(arg3))

得到CCD_ 1。

我有两个问题:

  1. 有没有一种方法可以在没有重复的显式joiner的情况下做到这一点字符((_)),并且对于可变长度的参数列表
  2. 有必要这样传递论点吗:

    (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)

关于:

#include <iostream>
#define COMPOSE(prefix,name) prefix##_##name

int main() {
    int COMPOSE(first,par);
    first_par = 1;
    return first_par;
}