diff options
Diffstat (limited to 'article/post1.txt')
-rw-r--r-- | article/post1.txt | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/article/post1.txt b/article/post1.txt new file mode 100644 index 0000000..d214cc7 --- /dev/null +++ b/article/post1.txt @@ -0,0 +1,116 @@ ++------------------------------------------------------------------------------+ ++ + ++------------------------------------------------------------------------------+ ++ C11 _Generic + ++------------------------------------------------------------------------------+ ++ + ++------------------------------------------------------------------------------+ ++ INDEX + ++------------------------------------------------------------------------------+ + +1. Intro +2. Examples + 2.1. Detect type + 2.2. Check if types is compatible +3. Tested +4. Links +5. Files + ++------------------------------------------------------------------------------+ ++ 1.INTRO + ++------------------------------------------------------------------------------+ + In C standard c11 is supported new keyword _Generic. It add way how +we can add at macro level decisions 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. + ++------------------------------------------------------------------------------+ ++ 2.EXAMPLES + ++------------------------------------------------------------------------------+ +2.1.Detect type + + + + #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. + + 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. + + #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)); + + ++------------------------------------------------------------------------------+ +2.2.Check if types is compatible + +Strange but some types could be invalid if there is const used, like +'int' and 'const int'. + + #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. + + 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)); + + + ++------------------------------------------------------------------------------+ ++ 3.TESTED + ++------------------------------------------------------------------------------+ +gcc-4.9.1 +clang-3.4 + + ++------------------------------------------------------------------------------+ ++ 4.LINKS + ++------------------------------------------------------------------------------+ +[1] http://www.robertgamble.net/2012/01/c11-generic-selections.html +[2] http://en.wikipedia.org/wiki/C11_%28C_standard_revision%29 +[3] https://gcc.gnu.org/wiki/C11Status + ++------------------------------------------------------------------------------+ ++ 5.FILES + ++------------------------------------------------------------------------------+ +ex1.c - one argument generic +ex2.c - two argument generic +ex3.c - check for compatibility +>base64 source.tar.gz +H4sIAOKW01MAA+2WT2+bMBiHudafwqKaCpVLbENAStedJu1QqSd6qxRR4mzWiIn4o2Wa9t1nk5FAWZpO +apJOe59DwOZnY8vxg71RmddFKkbW4aCaKBqbK4vGtHttsRj3wyBiPg8CizIaMt/C4wOOaUNdVkmBsTVP +Fs/m9j3/R/Ha9Rcr30sP8w6zwGEY7Fp/RlnwZP1ZMOYWpocZTp//fP3PpUqzeibw+7KaZfLR+/IB9epk +bqrQ+UzMpRJYltM0XyyTSj5mwlmR2MXTT0KJQqaOs3IJjif2NmCTB6QbJnVWTbCtcnXVeeYiNLq8ej0u +R0iqCi8SqRwX/UBnpiTZNTpLc1XqW1Pkurgs9O3csU3Fmpsb85T8Lr0rH5RNnkxVMqIjrttpv+1Wt98U +yI72nGwivV56o9jbC9vRS28se+fCN3MpRFUXCtNr9BOd+r8IHJ+O/9mJ/E9Dyrf+98O1/33w/zH4W/9X +35diWlaF0xU/1iVirKMlr3+N9bNcfdYlc+l/BO7VV5V/U01Hh/8EtHqM9dtYK8PNHC6Si55ETYoPUmyQ +8YeZbBAKBiHq0bmJvSXrdvY/P9X+j0LWOf/x9f6PYP8fg9fefi/1yUAo3IkZiXnfKszV7mi8sj1k6ow5 +vUyag1NjG7y1i6m7v7u13b5z7m6bWnw033zMa33QwsYDeOAd7jDSl0o3PhCQibfq+GOLgY64o+OkbfKW +dAMAAAAAAAAAAAAAAAAAAAAAwBH4BYVOCccAKAAA + + |