Sunday, January 15, 2012

C11 - Generic Selections

C99 introduced type-generic macros defined in the new tgmath.h header. These are macros that expand to the appropriate function based on the type of the provided argument. For example, their are 6 different arc cosine functions: 3 for the different real floating point types and 3 for the different complex floating point types. If you #include <tgmath.h>, a macro with the name acos is exposed which will expand to a call to one of the 6 functions based on the argument type.

The type-generic mechanism employed to make this work was not exposed or made available to the programmer but was instead left up to implementation magic. C11 introduces the generic selection expression which provides a mechanism to make compile-time choices based on type allowing type-generic macros to be created using standard C constructs.

In this post I will introduce and explore some of the applications of the new generic selection.

Overview

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).

The use of _Generic can be abstracted in a macro:
    #define type_idx(T) _Generic( (T), char: 1, int: 2, long: 3, default: 0)
So that type_idx('a') evaluates to 2 and type_idx("a") evaluates to 0.

Details

  • A generic selection consists of a controlling expression (which is not evaluated) and one or more, comma-separated, generic associations.
  • Each generic association consists of a type-name (or default) and a result expression separated by a colon.
  • The result of the generic selection expression is the result expression of the corresponding compatible type provided in the matching generic association.
  • If none of the provided types are compatible with the type of the controlling expression, a default selection is used if provided, otherwise the construct is erroneous.
  • The order of the generic associations in the list is inconsequential; no more than one compatible type may be provided in a generic selection so there is never more than one case that could match.
  • The type name in a generic association must be with a complete, non-VLA, type.
  • The controlling expression can be any expression.
  • The types of the result expressions can vary between the provided generic associations.

Examples

Type Generic Macros

The driving force behind _Generic is to provide a pseudo type-polymorphism mechanism. For example, the acos macro defined in tgmath.h could be implemented as:
#define acos(X) _Generic((X), \
    long double complex: cacosl, \
    double complex: cacos, \
    float complex: cacosf, \
    long double: acosl, \
    float: acosf, \
    default: acos \
    )(X)
This works well for single-argument functions but becomes more complex for multiple-argument functions. For example, tgmath.h provides a pow macro that expands to one of 6 functions but it accepts two arguments, both of which must be considered when determining the version of the function to call. A possible implementation for the pow macro is (empty lines added for readability):
#define pow(x, y) _Generic((x), \
    long double complex: cpowl, \

    double complex: _Generic((y), \
    long double complex: cpowl, \
    default: cpow), \

    float complex: _Generic((y), \
    long double complex: cpowl, \
    double complex: cpow, \
    default: cpowf), \

    long double: _Generic((y), \
    long double complex: cpowl, \
    double complex: cpow, \
    float complex: cpowf, \
    default: powl), \

    default: _Generic((y), \
    long double complex: cpowl, \
    double complex: cpow, \
    float complex: cpowf, \
    long double: powl, \
    default: pow), \

    float: _Generic((y), \
    long double complex: cpowl, \
    double complex: cpow, \
    float complex: cpowf, \
    long double: powl, \
    float: powf, \
    default: pow) \
    )(x, y)

Printing values generically

Similar to selecting the name of a function to call based on the argument type, we can select, for example, a printf format specifier based on type. This allows the creation of a macro that can print any type of value that printf supports without having to specify the type explicitly in the call:
#define printf_dec_format(x) _Generic((x), \
    char: "%c", \
    signed char: "%hhd", \
    unsigned char: "%hhu", \
    signed short: "%hd", \
    unsigned short: "%hu", \
    signed int: "%d", \
    unsigned int: "%u", \
    long int: "%ld", \
    unsigned long int: "%lu", \
    long long int: "%lld", \
    unsigned long long int: "%llu", \
    float: "%f", \
    double: "%f", \
    long double: "%Lf", \
    char *: "%s", \
    void *: "%p")

#define print(x) printf(printf_dec_format(x), x)
#define printnl(x) printf(printf_dec_format(x), x), printf("\n");
We can then print values like so:
    printnl('a');    // prints "97" (on an ASCII system)
    printnl((char)'a');  // prints "a"
    printnl(123);    // prints "123"
    printnl(1.234);      // prints "1.234000"
Note that since 'a' is an int and we set int up to use the "%d" format specifier, we have to cast it to a char if we want to print out the letter.

What about a string literal?
    printnl("abc");    // error on clang
This produces an error on clang. The problem is that "abc" has type char [4] (3 plus the nul terminator) which is not compatible with the char * type in our macro. An array of type is converted to a pointer to type except in certain cases as described in §6.3.2.1p3:

Except when it is the operand of the sizeof operator, the _Alignof operator, or the
unary & operator, or is a string literal used to initialize an array, an expression that has
type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points
to the initial element of the array object and is not an lvalue.

This clause makes no mention of _Generic so one would assume that the conversion should occur in this case, this may be a defect in clang.

So how do we handle it? We can't use char [] as the type name because char [] is not a complete type. If we wish to use this macro with a string literal (at least on clang), we will need a cast:
    printnl((char *)"abc");    // ok
Another problem occurs when we try to print a variable of qualified type:
    const int ci = 123;
    printnl(ci);     // error
The problem is that const int is not compatible with int so there is no valid generic association. We can either cast ci to char * or expand our macro to handle const types (and possibly volatile types and const volatile types as well).


Determine if an expression is compatible with a type

We can create a macro that evaluates to true if an expression is compatible with the provided type:
    #define is_compatible(x, T) _Generic((x), T:1, default: 0)
This can be useful for determining the underlying type of a typedef or enum:
    is_compatible((size_t){0}, unsigned long);
will evaluate to true if size_t is a typedef for unsigned long. Note that we can only compare an expression with a type, we cannot directly compare two expressions or two type names. If we want to compare two types, we can use the C99 compound literal to create a literal of a given type as we did above. There is no standard-conforming way to test two variables for type compatibility with this mechanism. On a compiler that supports the common typeof extension, something like this may work:
    is_compatible(x, typeof(y));
_Generic is evaluated at compile-time and can be used with _Static_assert if the result is an integer constant expression:
    enum e { E1; };
    _Static_assert(is_compatible(E1), int);  // always true
    _Static_assert(is_compatible((enum e){E1}, int);  // not necessarily true
It is possible to define a macro to ensure that an expression, perhaps a function argument, is of a given type:
    #define ensure_type(p, t) _Static_assert(is_compatible(p, t), \
    "incorrect type for parameter '" #p "', expected " #t)

Printing type names

As a final example, we can create a macro to expand to the name of a type that the macro recognizes. This can useful when trying to determine the type of a complex expression or the underlying type of a typedef:
/* Get the name of a type */
#define typename(x) _Generic((x), _Bool: "_Bool", \
    char: "char", \
    signed char: "signed char", \
    unsigned char: "unsigned char", \
    short int: "short int", \
    unsigned short int: "unsigned short int", \
    int: "int", \
    unsigned int: "unsigned int", \
    long int: "long int", \
    unsigned long int: "unsigned long int", \
    long long int: "long long int", \
    unsigned long long int: "unsigned long long int", \
    float: "float", \
    double: "double", \
    long double: "long double", \
    char *: "pointer to char", \
    void *: "pointer to void", \
    int *: "pointer to int", \
    default: "other")
void test_typename(void) {
    size_t s;
    ptrdiff_t p;
    intmax_t i;

    int ai[3] = {0};

    printf("size_t is '%s'\n", typename(s));
    printf("ptrdiff_t is '%s'\n", typename(p));
    printf("intmax_t is '%s'\n", typename(i));

    printf("character constant is '%s'\n", typename('0'));
    printf("0x7FFFFFFF is '%s'\n", typename(0x7FFFFFFF));
    printf("0xFFFFFFFF is '%s'\n", typename(0xFFFFFFFF));
    printf("0x7FFFFFFFU is '%s'\n", typename(0x7FFFFFFFU));
    printf("array of int is '%s'\n", typename(ai));
}
On my machine this prints:
size_t is 'unsigned long int'
ptrdiff_t is 'long int'
intmax_t is 'long int'
character constant is 'int'
0x7FFFFFFF is 'int'
0xFFFFFFFF is 'unsigned int'
0x7FFFFFFFU is 'unsigned int'
array of int is 'other'

117 comments:

  1. Which compilers support the _Generic keyword and which one have you used ?

    Regards Jörg

    ReplyDelete
  2. My testing was done on clang, I am not aware of any other mainstream compilers that support this yet.

    ReplyDelete
    Replies
    1. Try

      printnl("abc"+1);

      [ sizeof("a"); sizeof("a"+1); ]

      best regards
      h.o.b.s

      Delete
  3. Great article thanks.
    There is a typo in "type_idx('a') evaluates to 1". `type_idx('a') evaluates to 2.

    ReplyDelete
  4. I've enjoyed your article a lot. Very easy to follow and with lots of examples.
    Thank you

    ReplyDelete
  5. Thank you for such a great article.

    ReplyDelete
  6. I am appreciating your material.............

    web designing course

    ReplyDelete
  7. Great article. The two lines:

    _Static_assert(is_compatible(E1), int);
    _Static_assert(is_compatible((enum e){E1}, int);

    have typos: the parentheses are wrong, and the string literal is missing. (Confusingly, BOOST_STATIC_ASSERT() and msvc's _STATIC_ASSERT() don't permit the string literal.)

    Regards, jeq

    ReplyDelete
  8. PellesC supports _Generic and all C11 constructs

    ReplyDelete
  9. http://bantalsilikon01.blogspot.com/2014/12/a.html
    http://jualsangkarpleci.blogspot.com/2014/12/a.html
    http://kopiluwakliar01.blogspot.com/2014/12/a.html
    http://vvty.in/uncategorized/marinir-seo/

    http://marinirseo.blogspot.com/2014/12/a.html
    http://marinir-seo.blogspot.com/2014/12/a.html
    http://bumbu-pecel-malang.blogspot.com/2014/12/a.html
    http://distro-seo.blogspot.com/2014/12/a.html
    http://restoran-seo.blogspot.com/2014/12/a.html
    http://bantalmalangmurah.blogspot.com/2014/12/a.html
    http://distrobantal.blogspot.com/2014/12/a.html
    http://kesethandukmalang.blogspot.com/2014/12/a.html

    http://gurunyaitu.blogspot.com/2014/12/b.htmlhttp://kedirinyaindah.blogspot.com/2014/12/a.htmlhttp://bumbupecel617.blogspot.com/2014/12/w.htmlhttp://akaeoleh.blogspot.com/2014/12/v.htmlhttp://satudiasaja.blogspot.com/2014/12/u.htmlhttp://goodluckyaa.blogspot.com/2014/12/t.htmlhttp://songolas11.blogspot.com/2014/12/r.htmlhttp://yudistiramlg.blogspot.com/2014/12/q.htmlhttp://arjunapnh.blogspot.com/2014/12/p.htmlhttp://marinirseo987.blogspot.com/2014/12/o.htmlhttp://ayoikut1.blogspot.com/2014/12/n.htmlhttp://sempelblogger.blogspot.com/2014/12/m.htmlhttp://hondajass.blogspot.com/2014/12/k.htmlhttp://kesetnyakaki.blogspot.com/2014/12/j.htmlhttp://alwayssate.blogspot.com/2014/12/i.htmlhttp://bumbupecelmlg7.blogspot.com/2014/12/h.htmlhttp://bumbunyaseo.blogspot.com/2014/12/g.htmlhttp://bumbupecel100.blogspot.com/2014/12/f.htmlhttp://smartfren1.blogspot.com/2014/12/e.htmlhttp://bumbumalang1.blogspot.com/2014/12/d.htmlhttp://bumbupecelmalang7.blogspot.com/2014/12/c.htmlhttp://marinirseo7.blogspot.com/2014/12/b.htmlhttp://bloggermalang1.blogspot.com/2014/12/a.htmlhttp://asfrt111.blogspot.com/2015/01/a.htmlhttp://fgrt445.blogspot.com/2015/01/a.htmlhttp://grte231.blogspot.com/2015/01/a.htmlhttp://htyu412.blogspot.com/2015/01/asdgt.htmlhttp://fgr453.blogspot.com/2015/01/a.htmlhttp://asw145.blogspot.com/2015/01/a.htmlhttp://gtry145.blogspot.com/2015/01/a.htmlhttp://ase476.blogspot.com/2015/01/a.htmlhttp://10ouy.blogspot.com/2015/01/a.htmlhttp://s9yt.blogspot.com/2015/01/a.html,http://marinir-seo-konsultan-12a.blogspot.com/2014/10/aaa.htmlhttp://asdf-14.blogspot.com/2014/10/hhh.htmlhttp://vvtiin.blogspot.com/2014/10/iii.htmlhttp://marinir-seo-8.blogspot.com/2014/10/jjj.htmlhttp://bantaljkt1.blogspot.com/2014/10/sss.htmlhttp://dfes12.blogspot.com/2014/10/rrr.htmlhttp://seo-guru01.blogspot.com/2014/10/qqq.htmlhttp://jksbindo.blogspot.com/2014/10/ppp.htmlhttp://jkt-49.blogspot.com/2014/10/ooo.htmlhttp://seokonsultan1.blogspot.com/2014/10/nnn.htmlhttp://guru-seo-malang.blogspot.com/2014/10/mmm.htmlhttp://seo-aaa.blogspot.com/2014/10/lll.htmlhttp://seo-konsultasi-mudah.blogspot.com/2014/10/kkk.htmlhttp://bantal-silikon15.blogspot.com/2014/10/ggg.htmlhttp://agen-seo16.blogspot.com/2014/10/fff.htmlhttp://agen-seo17.blogspot.com/2014/10/eee_30.htmlhttp://agen-seo18.blogspot.com/2014/10/ddd.htmlhttp://agen-seo19.blogspot.com/2014/10/ccc.htmlhttp://agen-seo20.blogspot.com/2014/10/bbb.htmlhttp://bumbu-pecel01.blogspot.com/2014/10/xxx.htmlhttp://bantalasdf.blogspot.com/2014/12/seo-sem-seo.htmlhhttp://seo-malang-19.blogspot.com/2014/10/bbb.htmlhttp://agenseomalang18.blogspot.com/2014/10/ccc.htmlhttp://marinirseo17.blogspot.com/2014/10/ddd.htmlhttp://bumbupecel16.blogspot.com/2014/10/eee.htmlhttp://domainhosting15.blogspot.com/2014/10/fff.htmlhttp://bumbupecel15.blogspot.com/2014/10/ggg.htmlhttp://marinirseo14.blogspot.com/2014/10/hhh.htmlhttp://bantalsilikon12a.blogspot.com/2014/10/iii.htmlhttp://marinirseo12.blogspot.com/2014/10/jjj.htmlhttp://bumbu-pecel-hz.blogspot.com/2014/10/kkk.htmlhttp://marinir-seo-001.blogspot.com/2014/10/lll.htmlhttp://bantal-silikon-murah.blogspot.com/2014/10/mmm.htmlhttp://vvty-ine.blogspot.com/2014/10/nnn.htmlhttp://give-a-power.blogspot.com/2014/10/ooo.htmlhttp://bumbupecel-12.blogspot.com/2014/10/ppp.htmlhttp://marinir-seo-konsultan.blogspot.com/2014/10/qqq.htmlhttp://guru-seo-praktisi-123.blogspot.com/2014/10/rrr.htmlhttp://guru-seminar-online01.blogspot.com/2014/10/sss.htmlhttp://kopiluwakmalang11.blogspot.com/2014/10/ttt.html

    ReplyDelete
  10. Thanks for more information best company for Top3th.co.in Packers and Movers India provide moving services.
    Packers and Movers Navi Mumbai
    Packers and Movers Thane
    Packers and Movers in Navi Mumbai
    Packers and Movers in Thane

    ReplyDelete
  11. You can also do

    #define printnl(x) printf(printf_dec_format(x) "\n", x)

    to avoid creating two statements.

    ReplyDelete
  12. Here is the worthy reading articles I have to recommend to you. You would earn much useful information like me. cs lewis mere christianity

    ReplyDelete
  13. SEO Techniques that can help you to get high rank your website in all Search engine like Google, Bing, Yahoo etc and Alexa Ranking. For more Detail visit Click Here
    SEO Techniques

    Search Engine Optimization

    ReplyDelete
  14. This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog
    commission express franchise

    ReplyDelete
  15. Looks up greats! I enjoy because it is a nice and very effective article. Thank you, writer for writing like a nice article. This is very nice post! I will bookmark this blog.
    Biscoot
    Bajao
    Online Radio Stations

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. I appreciate you sharing this blog article.Really looking forward to read more. Keep writing.
    Prem ratan dhan payo kickass torrent

    ReplyDelete
  18. I appreciate you sharing this blog article.Thanks Again. Awesome.
    Prem ratan dhan payo torrent

    ReplyDelete
  19. Generally I don’t read article on blogs, but I would like to say that this write-up very compelled me to take
    a look at and do so! Your writing style has been amazed me.
    Thank you, very great post.
    Please visit our wonderful and valuable website-
    http://packersmoverschennai.in/
    http://packersmoverschennai.in/packers-and-movers-indian-institute-of-technology-chennai

    ReplyDelete
  20. The way of promotion in search engine and social media would help to reach more people for selling our web design services.
    Website Design Company Bangalore | Website Design Companies Bangalore

    ReplyDelete
  21. I appreciate you sharing this blog article. Keep writing.
    diwali sms

    ReplyDelete
  22. I truly appreciate this blog article.Really thank you! Great.
    Diwali wishes 2015

    ReplyDelete
  23. packers and movers pune In the event you have to shift nevertheless usually are over a restricted spending program, there are several steps to diminish the cost. In the event people organize ahead and bunch your tiny possessions, you'll be able to sacrifice a bit of cash over a shift.
    packers and movers gurgaon

    ReplyDelete
  24. packers and movers bangalore You need to select almost three specialist Gurgaon movers and packers and obtain fair assessments associated with transferring costs. Ask them to enter your own home and have some details about what exactly points you'll be able to bunch without a doubt not to mention any additional or maybe odd expenses. To be able to steer clear of tricks, doctor movers by means of industry affiliations, as an example, this Native Indian Moving & Safe-keeping Association.
    packers and movers noida

    ReplyDelete
  25. Packers and movers bhubaneswar accept evolved effectively hoped-for later on affairs supplier on selective formulations to bring forth shipping regarding ménage affairs secure. Their in-person master human beings force out for certain supervise material, packing material also equally unloading as well despatch affecting Packers and Movers in Pune affairs and supply superiority resolutions. Movers and packers pune became contemporaneous instrumentates likewise tools around to assume by great on laborious matters occurring by high apartments and as well bunch together also transportation this somebodies. On respect to whatsoever typecast of ménage shifting trusts you are able to expect their specific assistance.
    please click here
    Packers And Movers Bangalore
    Packers And Movers in Bangalore

    ReplyDelete
  26. Movers and Packers in Sonipat
    Packers and Movers in Mathura
    http://www.shiftingsolutions.in/packers-and-movers-sonipat.html
    http://www.shiftingsolutions.in/packers-and-movers-mathura.html

    ReplyDelete
  27. We are amongst the most reputed companies in India providing you at your doorstep speedy, efficient, cost-effective and reliable movers packers services, relocation and related logistics services in different parts of the country. Our head office in Gurgaon, Corporate Office in Mumbai and branches in all major cities of India including Bangalore, hyrabad, Chennai, Kolkata, Guwahati, Nagpur, Ahmedabad, Surat, Noida, Faridabad, Punjab, Badodhra manages comprehensive packers and movers solutions to meet the transportation and relocation needs of our clients in different parts of country. Whether it’s a household move, office move or corporate relocation, we are packers and movers that focus on the customers’ needs.PLEASE CALL @ 9999167600,9711215215
    Why Corporate Movers and Packers
    We provide an array of packers movers services ranging from Local Household Shifting, Residential & Commercial Relocation, Industrial Shifting, Freight Forwarding, Logistic Services, Car Carrier & Transportation, Truck Rentals, Warehousing and Storage services.
    Articles by
    movers and packers in gurgaon @ http://www.corporatemoversindia.com/
    packers and movers in gurgaon @ http://www.corporatemoversindia.com/
    packers movers in gurgaon @ http://www.corporatemoversindia.com/
    packers movers gurgaon @ http://www.corporatemoversindia.com/

    ReplyDelete
  28. Articles by
    movers and packers in gurgaon @ http://www.corporatemoversindia.com/
    packers and movers in gurgaon @ http://www.corporatemoversindia.com/
    packers movers in gurgaon @ http://www.corporatemoversindia.com/
    packers movers gurgaon @ http://www.corporatemoversindia.com/
    movers and packers gurgaon sector-56 @ http://www.corporatemoversindia.com/
    packers and movers in gurgaon sector 55 @ http://www.corporatemoversindia.com/
    packers & movers in gurgaon @ http://www.corporatemoversindia.com/
    movers and packers gurgaon sector 54 @ http://www.corporatemoversindia.com/
    packer and movers in gurgaon sector 53 @ http://www.corporatemoversindia.com/
    movers packers in gurgaon sector 43 @ http://www.corporatemoversindia.com/

    ReplyDelete
  29. This comment has been removed by the author.

    ReplyDelete
  30. The code was really helpful for me sir. Please add some more pdf versions thanks.
    Adele – Hello Lyrics-song lyrics

    Adele – Hello Lyrics

    ReplyDelete
  31. This comment has been removed by the author.

    ReplyDelete
  32. Are you looking for best packers and movers? Give a call to us.We have an updated database of verified packers and movers companies in India.

    Packers Movers Bangalore

    Bangalore Packers and Movers

    Packers Movers Bangalore

    Ezmove Bangalore

    packers in Bangalore

    ReplyDelete
  33. i THINK THERE IS SOME grammatical mistakes mothers day 2016 quotes and some spelling mistakes too Fathers day 2016 quotes and check iphone 7 too Apple iPhone 7 and soon cbse result is going to live on the website CBSE RESULT 2016

    ReplyDelete
  34. This comment has been removed by the author.

    ReplyDelete
  35. This comment has been removed by the author.

    ReplyDelete
  36. Good to know ,but i got some error on the coding ,i don't know if my web development coding is error or not.

    ReplyDelete
  37. will be referring a lot of friends about this.Keep blogging.

    High Pagerank Backlinks

    ReplyDelete
  38. Kasse neuesten und Bilder Tag 2016 der frisch Valentine, Zitate, Tapeten und alles über diese valentinstagsgeschenk für männer 2016

    ReplyDelete
  39. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work.
    Angel Investors India

    ReplyDelete