C11中_Generic的语法和示例用法

我听说C11添加了泛型。我已经用Google搜索了一下,看了一些文章,了解到有一个新的关键字(_Generic)等等。但我似乎无法掌握所有内容。


它类似于C#中的泛型或C ++中的模板吗?谁能给我关于C11泛型定义的简要说明,其语法和简单的示例用法示例?


慕无忌1623718
浏览 896回答 3
3回答

德玛西亚99

这是一个很好的介绍。这是概述:通用选择是通过新关键字_Generic实现的。语法类似于类型的简单switch语句:_Generic( 'a', char: 1, int: 2, long: 3, default: 0) 取值为2(字符常量在C中为int)。基本上,它的工作方式类似于的switch,其中标签是类型名称,将根据第一个表达式的类型('a'上述)进行测试。结果成为评估的结果_Generic()。

翻过高山走不出你

我看到的最好的例子启发了以下(可运行的)示例,该示例为破解内省开辟了各种奇特的可能性...#include <stdio.h>#include <stddef.h>#include <stdint.h>#define typename(x) _Generic((x),&nbsp; &nbsp; &nbsp; &nbsp; /* Get the name of a type */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; &nbsp; _Bool: "_Bool",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unsigned char: "unsigned char",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char: "char",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;signed char: "signed char",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; short int: "short int",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;unsigned short int: "unsigned short int",&nbsp; &nbsp; &nbsp;\&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int: "int",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;unsigned int: "unsigned int",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; &nbsp; &nbsp;long int: "long int",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;unsigned long int: "unsigned long int",&nbsp; &nbsp; &nbsp; \long long int: "long long int", unsigned long long int: "unsigned long long int", \&nbsp; &nbsp; &nbsp; &nbsp; float: "float",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double: "double",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; long double: "long double",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char *: "pointer to char",&nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; &nbsp;void *: "pointer to void",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int *: "pointer to int",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; &nbsp; &nbsp; default: "other")#define fmt "%20s is '%s'\n"int main() {&nbsp; size_t s; ptrdiff_t p; intmax_t i; int ai[3] = {0}; return printf( fmt fmt fmt fmt fmt fmt fmt fmt,&nbsp; &nbsp; &nbsp;"size_t", typename(s),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"ptrdiff_t", typename(p),&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;"intmax_t", typename(i),&nbsp; &nbsp; &nbsp; "character constant", typename('0'),&nbsp;"0x7FFFFFFF", typename(0x7FFFFFFF),&nbsp; &nbsp; &nbsp;"0xFFFFFFFF", typename(0xFFFFFFFF),"0x7FFFFFFFU", typename(0x7FFFFFFFU),&nbsp; "array of int", typename(ai));}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;╔═══════════════╗&nbsp;═════════════════╣ Amazeballs... ╠═════════════════════════════════════&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;╚═══════════════╝&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size_t is 'unsigned long int'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ptrdiff_t is 'long int'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intmax_t is 'long int'character constant is 'int'&nbsp; &nbsp; &nbsp; &nbsp; 0x7FFFFFFF is 'int'&nbsp; &nbsp; &nbsp; &nbsp; 0xFFFFFFFF is 'unsigned int'&nbsp; &nbsp; &nbsp; &nbsp;0x7FFFFFFFU is 'unsigned int'&nbsp; &nbsp; &nbsp; array of int is 'other'

白猪掌柜的

我使用的是clion 1.2.4,而clion现在不支持c11,因此我在c99中使用以下代码而不是_Generic#include <stdio.h>int main(int argc, char **argv) {&nbsp; &nbsp; char *s;&nbsp; &nbsp; if (__builtin_types_compatible_p(__typeof__(s), long)) {&nbsp; &nbsp; &nbsp; &nbsp; puts("long");&nbsp; &nbsp; } else if (__builtin_types_compatible_p(__typeof__(s), char*)) {&nbsp; &nbsp; &nbsp; &nbsp; puts("str");&nbsp; &nbsp; }&nbsp; &nbsp; return (0);};
打开App,查看更多内容
随时随地看视频慕课网APP