在C11 _Generic的语法和用法示例 [英] Syntax and Sample Usage of _Generic in C11
问题描述
我听说C11增加仿制药。我GOOGLE了一下,看了一些文章,了解有一个新的关键字( _Generic
)和所有。但我似乎无法掌握这一切。
I heard C11 added generics. I've googled a bit, looked at some articles, understood there's a new keyword ( _Generic
) and all. But I can't seem to grasp it all.
是不是像在C#泛型或模板的C ++?谁能给我泛型的C11定义的简要说明,它的语法和一个简单的示例使用例子?
Is it something like the generics in C# or templates in C++? Can anyone give me a brief explanation of the C11 definition of generics, its syntax and a simple sample usage example?
推荐答案
这是pretty很好的介绍。这里是概述:
This is a pretty good introduction. Here's the Overview:
通用选择一个新的关键字来实现:_Generic。该语法类似于一个
对于类型的简单switch语句: _Generic('A',焦炭:1,中间体:2,长:3,默认:0)
计算结果为2(字符常量是在整数C)。
Generic selection is implemented with a new keyword: _Generic. The syntax is similar to a simple switch statement for types:
_Generic( 'a', char: 1, int: 2, long: 3, default: 0)
evaluates to 2 (character constants are ints in C).
基本上,它就像一种开关
,这里的标记是是针对第一八佰伴pression的型式试验类型的名称(<$ C的$ C>'A'以上)。结果成为评估的结果 _Generic()
。
Basically it works like a kind of switch
, where the labels are type names which are tested against the type of the first expression (the 'a'
above). The result becomes the result of evaluating the _Generic()
.
这篇关于在C11 _Generic的语法和用法示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!