默认参数
#include <stdio.h>
#define VA_NUM_ARGS_IMPL(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, \
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, \
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, XX, ...) XX
#define VA_NUM_ARGS(...) \
VA_NUM_ARGS_IMPL("ignored", ##__VA_ARGS__, \
Z, Y, X, W, V, U, T, S, R, Q, P, O, N, M, L, K, J, I, H, G, F, E, D, C, B, A, \
z, y, x, w, v, u, t, s, r, q, p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a, \
9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define __CONNECT2(a, b) a##b
#define CONNECT2(a, b) __CONNECT2(a, b)
#define DISPATCH(prefix, ...) \
CONNECT2(prefix, VA_NUM_ARGS(__VA_ARGS__))
void func_0()
{
puts(__func__);
}
void func_1(int)
{
puts(__func__);
}
void func_2(int, int)
{
puts(__func__);
}
void func_3(int, int, int)
{
puts(__func__);
}
// more func_N ...
#define func(...) DISPATCH(func_, __VA_ARGS__)(__VA_ARGS__)
int main(int argc, char const *argv[])
{
func();
func(1);
func(1, 2);
func(1, 2, 3);
return 0;
}
标签:__,VA,int,ARGS,默认,参数,func,可变,define
From: https://www.cnblogs.com/sfdevs/p/17368840.html