【发布时间】:2021-03-15 14:34:22
【问题描述】:
我正在尝试解决有关具有多个版本的协议的问题。 它的结构版本会不断扩展,像下面的sn-p,1.1.2版本可能会来。
原文:
#include <stdio.h>
#include <stdint.h>
typedef struct GetReading {
uint8_t val;
} GetReading_T;
int main()
{
GetReading_T instaince = {0};
instaince.val = 0x12c;
printf("%02x\n", instaince.val);
return 0;
}
之后:
#include <stdio.h>
#include <stdint.h>
#define MULTI_S(NAME, VERSION) \
typedef struct NAME##_##VERSION {
#define MULTI_E(NAME, VERSION) \
} NAME##_##VERSION##_t;
MULTI_S(GetReading, 1_1_0)
uint8_t val;
MULTI_E(GetReading, 1_1_0)
MULTI_S(GetReading, 1_1_1)
uint16_t val;
MULTI_E(GetReading, 1_1_1)
/*
typedef struct GetReading_1_1_0 {
uint8_t val;
} GetReading_1_1_0_t;
typedef struct GetReading_1_1_1 {
uint16_t val;
} GetReading_1_1_1_t;
*/
typedef union GetReading {
GetReading_1_1_0_t v110;
GetReading_1_1_1_t v111;
} GetReading_T;
int main()
{
GetReading_T instaince = {0};
instaince.v111.val = 0x12c;
int type = 1;
if(type == 1) {
printf("%04x\n", instaince.v111.val);
} else if (type == 2) {
printf("%02x\n", instaince.v110.val);
}
return 0;
}
我只是想知道这个宏技术是否太难阅读并且可能是多余的,因为它可以通过直接声明来实现相同的效果。而且我发现很难用当前的实现来扭曲宏。
C/C++有没有更好的做法来解决这类问题?
【问题讨论】:
-
对我来说似乎非常混乱和冗长。我看不出将名称直接放入宏而不是宏有什么问题,因为这样会不那么冗长并且更容易理解恕我直言。
-
是的,这似乎是不必要的复杂。如果只有 2 个版本,那么显然不需要抽象层。老派的解决方案是一个带有枚举类型的结构,然后是一个包含数据的联合成员。
-
@Lundin 知道了,我怀疑它将来会有大约5个版本用于一个类似的结构,并且有很多不同的结构有很多成员,最初我想使用宏因为它会让我更容易知道已经实现了哪些具有多个版本的结构。
标签: c c-preprocessor