summaryrefslogtreecommitdiffstats
path: root/md/writeup/c_c11_standard_generic_keyword.md
blob: 639264cd012882dca891ed66053d2418ec1aa718 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
title:C C11 standart _Generic keyword
keywords:c,c11,generic


# C C11 standart _Generic keyword

## INTRO


In C standart c11 is supported new keyword _Generic. It add way how
we can add at macro level decisitions about variable types. And now is
possible make some C++ like function "overloading". Here is some variants how
to use this generic "_Generic". Now could print variables dont looking
on its type _Generic will deal with it.


## EXAMPLES

Detect type

```c
#define type_str(T) _Generic( (T), int: "int",\
long: "long",\
default: "Unknown type")
```

Now we have define check for 2 types int and long. If there is some type
undefined then choose default type and print "Unknown type". This is auto
printf example where you can detect type and print its name. But as in page [1]
you can add number values and compare types in simple way.

```c
printf("Type1 %s\n", type_str('a'));
printf("Type2 %s\n", type_str(1));
printf("Type3 %s\n", type_str(1l));
printf("Type4 %s\n", type_str(0.0f));
```

Also is possible to use generic for 2 or more types but then amount
of declaration grows. But it means that now according to params
we can choose best function.

```c
#define type_str2(T1,T2) _Generic( (T1),\
int: _Generic((T2),int:"int int", default: "int UNK"),\
default: "UNK UNK" )
 
printf("Double Type 1 %s\n", type_str2(1,1));
printf("Double Type 2 %s\n", type_str2(1,0.0f));
printf("Double Type 3 %s\n", type_str2(.0f,.0f));
```

Check if types is compatible


Strange but some types could be invalid if there is const used, like
'int' and 'const int'.

```c
#define is_compatible(x,T) _Generic((x), T:"compatible",\
default: "non-compatible")
```

Here is defined 2 types and only (int and int) and (const int and const int)
is compatible.

```c
int i1;
const int i2;
printf("int       == int,       %s\n", is_compatible(i1,int));
printf("const int == const int, %s\n", is_compatible(i2,const int));
printf("int       == const int, %s\n", is_compatible(i1,const int));
printf("const int == int,       %s\n", is_compatible(i2,int));
```



## TESTED

gcc-4.9.1  
clang-3.4


## FILES

ex1.c - one argument generic  
ex2.c - two argument generic  
ex3.c - check for compatibility


## Links
http://www.robertgamble.net/2012/01/c11-generic-selections.html  
http://en.wikipedia.org/wiki/C11_%28C_standard_revision%29  
https://gcc.gnu.org/wiki/C11Status  

## Downloads
ex3.c -
1KiB - http://archive.main.lv/files/writeup/c_c11_standart__generic_keyword/ex3.c  
ex1.c -
1KiB - http://archive.main.lv/files/writeup/c_c11_standart__generic_keyword/ex1.c  
ex2.c - 
1KiB - http://archive.main.lv/files/writeup/c_c11_standart__generic_keyword/ex2.c  
source.tar.gz -
1KiB - http://archive.main.lv/files/writeup/c_c11_standart__generic_keyword/source.tar.gz