0

我这里有一点复制/粘贴问题。我有一些数据类型,我根据我需要的位数来定义类型。我使用的是 8 位 MCU,因此使用数据适合的最小类型很重要。

代码最终看起来像这样:

#ifndef INT24_MAX 
// For portability
typedef int32_t int24_t
#define int24_t int24_t
#endif

#if MY_BITS <= 8
typedef int8_t MY_TYPE_T;
#warning MY_TYPE_T is int8_t

#elif MY_BITS <= 16
typedef int16_t MY_TYPE_T;
#warning MY_TYPE_T is int16_t

#elif MY_BITS <= 24
typedef int24_t MY_TYPE_T;
#warning MY_TYPE_T is int24_t

#elif MY_BITS <= 32
typedef int32_t MY_TYPE_T;
#warning MY_TYPE_T is int32_t

#else
#error MY_TYPE_T is too big.
#endif

我不喜欢这些行:

typedef int16_t MY_TYPE_T;
#warning MY_TYPE_T is int16_t

因为 int16_t 和 MY_TYPE_T 是复制粘贴的。此外,由于我在多个变量上使用它,因此存在复制/粘贴问题,因为我在 MY_TYPE_2_T 和 MY_TYPE_3_t 中重复相同的代码(注意:实际上并未在项目中命名这些“MY_TYPE”)。

我正在寻找一种方法来根据位数对类型进行 typedef 并输出有关该类型的消息,或者如果类型变得太大则给出错误。

//Something like this
#define MY_BITS (10 + 2)
Int_BestFit(MY_TYPE_N, MY_BITS);
4

0 回答 0