|
发表于 2022-5-27 23:32:58
|
显示全部楼层
本帖最后由 XIVN1987 于 2022-5-27 23:39 编辑
根据下面这个帖子里的说法,#if的条件不能包含sizeof
https://stackoverflow.com/questions/4079243/how-can-i-use-sizeof-in-a-preprocessor-macro
如果编译器版本大于等于C11,可以使用 _Static_assert:_Static_assert(sizeof(DEVICE_PARA) == 24, "size is not correct");
如果编译器版本小于C11,那就只能自己定义个宏,,比如tinyusb里面是这么搞的
- #define TU_STRCAT(a, b) a##b ///< concat without expand
- #define TU_XSTRCAT(a, b) TU_STRCAT(a, b) ///< expand then concat
- // Compile-time Assert
- #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
- #define TU_VERIFY_STATIC _Static_assert
- #else
- #define TU_VERIFY_STATIC(const_expr, _mess) enum { TU_XSTRCAT(_verify_static_, __LINE__) = 1/(!!(const_expr)) }
- #endif
- TU_VERIFY_STATIC(sizeof(DEVICE_PARA) == 24, "size is not correct");
复制代码 |
|