gpt4 book ai didi

c++ - 避免 if else 打印 block 中的代码重复

转载 作者:行者123 更新时间:2023-11-28 05:48:57 25 4
gpt4 key购买 nike

我讨厌这里的代码重复。如何使它更简洁?重复文本的另一个宏是个好主意吗?我有一种奇怪的感觉,我也遗漏了一些非常明显的东西。

      #define CHECK(para_1, para_2, para_3, epic_fail) { \
if (para_2 != para_3) { \
size_t __bytes; \
if(#para_1 == "special") { \
__bytes = snprintf( \
NULL, 0, \
#para_1 \
" this_thing '%s'(%p) doesn't go well with special this_thing '%s'(%p)", \
para_2, para_2, para_3, para_3); \
} else { \
__bytes = snprintf( \
NULL, 0, \
#para_1 " this_thing '%s'(%p) doesn't go well with unique_goldfish this_thing '%s'(%p)", \
para_2, para_2, para_3, para_3); \
} \
char * message = \
reinterpret_cast<char *>(rmw_allocate(__bytes + 1)); \
if(#para_1 == "special") { \
snprintf( \
message, __bytes + 1, \
#para_1 \
" this_thing '%s'(%p) doesn't go well with special this_thing '%s'(%p)", \
para_2, para_2, para_3, para_3); \
} else{ \
snprintf( \
message, __bytes + 1, \
#para_1 " this_thing '%s'(%p) doesn't go well with unique_goldfish this_thing '%s'(%p)", \
para_2, para_2, para_3, para_3); \
} \
SET_THE_ERROR(message); \
free_the_mem(message); \
epic_fail; \
} \
}

最佳答案

在宏中使用宏:

#define CHECK_PRINT(n) snprintf( \
message, __bytes + n, \
#para_1 " this_thing '%s'(%p) doesn't go well with unique_goldfish this_thing '%s'(%p)", \
para_2, para_2, para_3, para_3);

#define CHECK(para_1, para_2, para_3, epic_fail) { \
if (para_2 != para_3) { \
size_t __bytes; \
if(#para_1 == "special") { \
__bytes = CHECK_PRINT(n); \
} else { \
__bytes = CHECK_PRINT(n); \
} \
...

关于c++ - 避免 if else 打印 block 中的代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35667061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com