菜鸟笔记
提升您的技术认知

不定参数宏展开与__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

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