我想做一个宏来判断给定的值是否是整数,我发现有些整数类型在使用_Generic时是相互兼容的,所以它们不能都作为单独的_Generic情况使用.

比如intint32_t

signed charint8_t

what is the rule on that? Is there a solution that w或ks with all platf或ms/compilers?

我当前的代码是:

#define is_integer(exp) \
_Generic(exp, \
    int8_t: 1, \
    int16_t: 1, \
    int32_t: 1, \
    int64_t: 1, \
    uint8_t: 1, \
    uint16_t: 1, \
    uint32_t: 1, \
    uint64_t: 1, \
    default: 0)

However, it doesn't w或k with char (goes to default), but w或ks with signed char (goes to int8_t).

Should I add char as a separate _Generic case? I'm afraid that some implementation typedefs int8_t to char so it won't w或k, 或 are they not allowed to?

推荐答案

有没有适用于所有平台/编译器的解决方案?

(u)intN_toptional种类型.

在使用通常的(u)intN_t种类型的实现上,请考虑两阶段_Generic.

#define is_integer2(exp) \
_Generic(exp, \
    int8_t: 1, \
    int16_t: 1, \
    int32_t: 1, \
    int64_t: 1, \
    uint8_t: 1, \
    uint16_t: 1, \
    uint32_t: 1, \
    uint64_t: 1, \
    default: 0)

#define is_integer1(exp) \
_Generic(exp, \
    char: 1, \
    signed char: 1, \
    unsigned char: 1, \
    ...  // 2 shorts, int, unsigned, 2 longs
    long long: 1, \
    unsigned long long: 1, \
    default: is_integer2(exp))

代码可以在另一个级别中包括(u)intptr_t.

代码可以在另一个级别中包括size_t, ptrdif_t.

代码可以在另一个级别中包括(u)intmax_t.

等.

C++相关问答推荐

librsvg rsvg_handle_get_dimensions获取像素大小与浏览器中的渲染大小没有不同

malloc实现:判断正确的分配对齐

VS代码C/C++扩展intellisense无法检测环境特定函数'

GCC引发不明确的诊断消息

ARM64 ASIMD固有的加载uint8_t* 到uint16x8(x3)?

二进制计算器与gmp

我在这里正确地解释了C操作顺序吗?

当输入负数时,排序算法存在问题

Make Node函数.S有什么问题吗?

如何在C中打印包含扫描字符和整数的语句?

对于C中给定数组中的每个查询,如何正确编码以输出给定索引范围(1到N)中所有数字的总和?

GTK函数调用将完全不相关的char* 值搞乱

如何只获取字符串的第一个单词,然后将其与c中的另一个单词进行比较?

递归打印二维数组(C编程)

如何摆脱-WIMPLICIT-Function-声明

在我的第一个C语言中观察到的错误';你好世界';程序

计算时出现奇怪的计算错误;N Select K;在C中

在分配内存后使用指针是未定义的行为吗?

模仿 memmove 的行为

如何确保 gcc + libc 对于多字节字符串使用 UTF-8,对于 wchar_t 使用 UTF-32?