64,641
社区成员
发帖
与我相关
我的任务
分享
// 说明1, VC中宏参数显示包含(), 那么()连同里面内容都都作为一个参数, 到完全展开为止
// 说明2, VC中宏参数显示包含(),那么宏中的所有宏(所有后裔宏)的最后一级展开为最后时间一起展开
// 如上, 必要时, 可使用 左右括符 符号, 使()为非显示
#define BRACKET_L (
#define BRACKET_R )
// 宏 PP_NARG(...) 获得 参数个数, 参数最大数量 63
#define PP_RSEQ_N \
63,62,61,60, \
59,58,57,56,55,54,53,52,51,50, \
49,48,47,46,45,44,43,42,41,40, \
39,38,37,36,35,34,33,32,31,30, \
29,28,27,26,25,24,23,22,21,20, \
19,18,17,16,15,14,13,12,11,10, \
9,8,7,6,5,4,3,2,1,0
#define PP_ARG_N( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N
#define PP_NARG(...) \
PP_ARG_N BRACKET_L __VA_ARGS__,PP_RSEQ_N BRACKET_R
// 宏 CONNECT(a, b) 将两个参数串为一个
#define CONNECT1(a, b) a##b
#define CONNECT(a, b) CONNECT1(a, b)
// 宏 EXPAND_NARG(name, __VA_ARGS__) 将 name 与 参数个数 串为一个, 如 EXPAND_NARG(name, 5, 6, 7, 8, 9) 为 name5
#define EXPAND_NARG(name, __VA_ARGS__) \
CONNECT BRACKET_L name, PP_NARG(__VA_ARGS__) BRACKET_R (__VA_ARGS__)
// 宏 PRINTFormatDis 将类似 ("%d, ", 1, "%d, ", 2, "%d, ", 3 ) 串为 "%d, %d, %d, %d, ", 1, 2, 3, 4
// 注意, 格式必须正确, 不然发生意想不到的事
#define PRINTFormatDis_2(a, A, ...) a, __VA_ARGS__, A
#define PRINTFormatDis_4(a, A, b, B, ...) PRINTFormatDis_2 BRACKET_L a##b, B, __VA_ARGS__, A BRACKET_R
#define PRINTFormatDis_6(a, A, b, B, ...) PRINTFormatDis_4 BRACKET_L a##b, B, __VA_ARGS__, A BRACKET_R
#define PRINTFormatDis_8(a, A, b, B, ...) PRINTFormatDis_6 BRACKET_L a##b, B, __VA_ARGS__, A BRACKET_R
#define PRINTFormatDis_10(a, A, b, B, ...) PRINTFormatDis_8 BRACKET_L a##b, B, __VA_ARGS__, A BRACKET_R
#define PRINTFormatDis_12(a, A, b, B, ...) PRINTFormatDis_10 BRACKET_L a##b, B, __VA_ARGS__, A BRACKET_R
#define PRINTFormatDis_14(a, A, b, B, ...) PRINTFormatDis_12 BRACKET_L a##b, B, __VA_ARGS__, A BRACKET_R
#define PRINTFormatDis_16(a, A, b, B, ...) PRINTFormatDis_14 BRACKET_L a##b, B, __VA_ARGS__, A BRACKET_R
#define PRINTFormatDis_18(a, A, b, B, ...) PRINTFormatDis_16 BRACKET_L a##b, B, __VA_ARGS__, A BRACKET_R
#define PRINTFormatDis_20(a, A, b, B, ...) PRINTFormatDis_18 BRACKET_L a##b, B, __VA_ARGS__, A BRACKET_R
#define PRINTFormatDis(...) EXPAND_NARG(PRINTFormatDis_, __VA_ARGS__)
//
#define MACRO_2(A, a) PRINT(#A, a)
#define MACRO_4(A, a, B, b) PRINT(#A#B, a, b)
#define MACRO_6(A, a, B, b, C, c) PRINT(#A#B#C, a, b, c)
#define MACRO_8(A, a, B, b, C, c, D, d) PRINT(#A#B#C#D, a, b, c, d)
#define MACRO_II(N, ARGS) MACRO_## N ## ARGS
#define MACRO_I(N, ARGS) MACRO_II(N, ARGS)
#define MACRO(...) MACRO_I(_VARIADIC_SIZE(__VA_ARGS__), (__VA_ARGS__))
#define _VARIADIC_SIZE(...) _VARIADIC_SIZE_EXPAND(_VARIADIC_SIZE_I(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0), )
#define _VARIADIC_SIZE_I(e0, e1, e2, e3, e4, e5, e6, e7, size, ...) size
#define _VARIADIC_SIZE_EXPAND(a, b) a
int a = 1;
MACRO(%d, a);
float b = 2.0f;
MACRO(%d, a, %f, b);
char c = '?';
MACRO(%d, a, %f, b, %c, c);
char const *d = "i am string";
MACRO(%d, a, %f, b, %c, c, %s, d);
#include <cstdio>
#include <utility>
void print(FILE* fp){
}
template<typename T,typename... Args>
void print(FILE* fp,T&& t,char const* fmt, Args... args){
fprintf(fp,fmt, std::forward<T>(t));
print(fp,std::forward<Args>(args)...);
}
int main() {
print(stdout, 10, "%d", 2.2, ",%f", 'H', ",%c","hello", ",%s\n");
return 0;
}