在preprocessor取下不断投 [英] Remove cast from constant in preprocessor
问题描述
在一个微控制器code,我利用那里有许多定义常量生产者提供一个图书馆。我想如果有我的一些常数之间的不匹配(与微控制器外组件共享,以 git的子树
)和微控制器常量给一个错误。
In a microcontroller code, I am using a library provided by the producer where there are many constants defined. I'm trying to give an error if there's a mismatch between some of my constants (shared with components outside the microcontroller, with git-subtree
) and the microcontroller constants.
例如,该库定义:
#ifdef SOME_PARTICULAR_MODEL
#define FLASH_BLOCK_SIZE ((uint8_t)64)
/* else, other models */
#endif
和地方,在微控制器code和在PC上整理了一些code之间共享的头,我有,例如:
And somewhere, in a header shared between the microcontroller code and some code compiled on the PC, I have for example:
#define MYPROG_BLOCK_SIZE 64
,并确保这些常量匹配,在微控制器code,其中两个常量存在,我有:
And to make sure these constants match, in the microcontroller code, where both constants exist, I have:
#if MYPROG_BLOCK_SIZE != FLASH_BLOCK_SIZE
#error "mismatch between actual block size and defined block size"
#endif
这是为了确保如果code是不断移植到一个更大的微控制器,共享的头也将被更新。
This is to make sure if the code is ever ported to a bigger microcontroller, the shared header would also be updated.
的问题是,这被简化为:
The problem is that this gets reduced to:
#if 64 != ((uint8_t)64)
这我不知道,如果是有效的 C ,但仍然让编译器呛。测试中,我发现,这个问题是不是 uint8_t有
是一个typedef,它仍然带有强制转换为 INT
例如
which I'm not sure if is valid C, but nonetheless makes the compiler choke up. Testing, I found out that the problem is not that uint8_t
is a typedef and it still chokes up with a cast to int
for example.
有没有一种方法,从定义为((uint8_t有)64)
(uint8_t有)部分>?如果没有,有没有办法让前pression变成一个没有一个投去改变它?
Is there a way to remove the (uint8_t)
part from a value defined as ((uint8_t)64)
? If not, is there any way to change it so the expression turns into one without a cast?
我想过定义 uint8_t有
的东西和#如果
,但我想不出后取消定义它出如何避免的投性质(Y)X
键,把它变成一个算术前pression。
I thought about defining uint8_t
as something and undefining it after the #if
, but I can't figure out how I can avoid the cast nature of (Y)X
and turn it into an arithmetic expression.
推荐答案
下面是一个改进版本(第一个版本是下文)。这其中不依赖于投是 uint8_t有
;它将与形式的任何 FLASH_BLOCK_SIZE
更换工作列表((某些类型的)数)
。
Here is an improved version (the first version is below). This one does not depend on the cast being uint8_t
; it will work with any FLASH_BLOCK_SIZE
replacement list of the form ((some type) number)
.
#define MYPROG_BLOCK_SIZE 64
#define FLASH_BLOCK_SIZE ((uint8_t)64)
#define B(x)
#define C(x) B x
#define D(x) C x
#if MYPROG_BLOCK_SIZE != D(FLASH_BLOCK_SIZE)
#error "mismatch between actual block size and defined block size"
#endif
下面是原始版本:
#define MYPROG_BLOCK_SIZE 64
#define FLASH_BLOCK_SIZE ((uint8_t)64)
#define uint8_t
#define Helper(x) x
#define Deparenthesize(x) Helper x
#if MYPROG_BLOCK_SIZE != Deparenthesize(Deparenthesize(FLASH_BLOCK_SIZE))
#error "mismatch between actual block size and defined block size"
#endif
#undef uint8_t
在写code,我会preFER一个静态断言,但上面做什么,你在preprocessor要求。
When writing code, I would prefer a static assert, but the above does what you requested in the preprocessor.
这篇关于在preprocessor取下不断投的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!