不定参数宏展开与__VA_ARGS__

引入

JSON for Modern C++ 中为方便序列化和反序列化定义了两宏,如下

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(name, member1, member2, ...) 将在要为其创建代码的类/结构的命名空间内定义。

NLOHMANN_DEFINE_TYPE_INTRUSIVE(name, member1, member2, ...) 将在要为其创建代码的类/结构中定义。 该宏还可以访问私有成员。

进一步查看代码,json.hpp文件 https://github.com/lichangke/Demos/blob/master/JsonDemo/MacrosForSerialization/json.hpp

/*!
@brief macro
@def NLOHMANN_DEFINE_TYPE_INTRUSIVE
@since version 3.9.0
*/
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...)  \
    friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
    friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }

/*!
@brief macro
@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
@since version 3.9.0
*/
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...)  \
    inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
    inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }

这里用到了 可变参数宏的展开 与 __VA_ARGS__ 相关

可变参数宏_VA_ARGS_

__VA_ARGS__可以将宏中的参数…展开

#include <iostream>
#define F(...) f(__VA_ARGS__)

int f(int a,int b,int c){
    return a+b+c;
}
int f(int a,int b){
    return a-b;
}
int main() {
    std::cout << "Hello, World!" << std::endl;
    std::cout<< F(1,2,3) <<std::endl; // 调用 int f(int a,int b,int c)
    std::cout<< F(1,2) <<std::endl; // 调用 int f(int a,int b)
    return 0;
}

输出:

Hello, World!
6
-1

Process finished with exit code 0

宏函数的重载

代码VariableParamMacro

头文件

#include <iostream>

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define PRINT(...) GET_MACRO(__VA_ARGS__,  OUTPUT3, OUTPUT2, OUTPUT1, ...) (__VA_ARGS__)
#define OUTPUT1(v1)  std::cout << v1 << std::endl;
#define OUTPUT2(v1, v2) OUTPUT1(v1) OUTPUT1(v2)
#define OUTPUT3(v1, v2, v3) OUTPUT1(v1) OUTPUT2(v2,v3)
#include <iostream>
#include "macroDemo.h"


int main() {
    std::cout << "Hello, World!" << std::endl;
    PRINT(1)
    std::cout << "==========" << std::endl;
    PRINT(1, 2)
    std::cout << "==========" << std::endl;
    PRINT(1, 2, 3)
    //PRINT(1, 2, 3, 4) // 超过宏定义中参数个数
    return 0;
}

输出

Hello, World!
1
==========
1
2
==========
1
2
3
Process finished with exit code 0

展开步骤:

对于 PRINT(1) :

  • 1、PRINT(1) -> GET_MACRO(1, OUTPUT3, OUTPUT2, OUTPUT1, …) (1)

  • 2、GET_MACRO(1, OUTPUT3, OUTPUT2, OUTPUT1, …) -> GET_MACRO(_1, _2, _3, NAME, …) NAME -> 得到 NAME 为 OUTPUT1

  • 3、回到 1 得到 PRINT(1) ->OUTPUT1(1)

对于PRINT(1,2,3) :

  • 1、PRINT(1,2,3) -> GET_MACRO(1,2,3, OUTPUT3, OUTPUT2, OUTPUT1, …) (1,2,3)
  • 2、GET_MACRO(1,2,3, OUTPUT3, OUTPUT2, OUTPUT1, …) -> GET_MACRO(_1, _2, _3, NAME, …) NAME -> 得到 NAME 为 OUTPUT3
  • 3、回到 1 得到 PRINT(1) ->OUTPUT3(1,2,3)

相对于这里JSON for Modern C++中的处理还在外面套了一层,封装了 func

希望我的文章对于大家有帮助,由于个人能力的局限性,文中可能存在一些问题,欢迎指正、补充!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值