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

标签 c++ c c-preprocessor

有没有办法让 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

我有两个问题:

  1. 没有重复的显式连接器有没有办法做到这一点 字符 ((_)) 和可变长度的参数列表?
  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)

关于C++ 预处理器——连接参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31076419/

相关文章:

c++ - c++ 友元函数的问题

c++ - 使用 QImage::loadFromData 将 cv::mat 转换为 QImage

c++ - fatal error : openssl/rsa. h:没有那个文件或目录

c++ - #define 导致 "expected primary-expression"错误

C# 预处理器指令

c++ - 从 future 对象数组中检索值,std::vector

c++ - 如何与其他 boost 库一起构建 boost::fiber?

android - opensl es : audio callback patterns

c - 自引用结构,包含三个指针(分别称为左指针、右指针和父指针)

c - 内联方法名称的 GCC 预处理器