From: tr.17687@z991.linuxsc.com   
      
   Keith Thompson writes:   
      
   > David Brown writes:   
   > [...]   
   >   
   >> How many people do you know who have actually written and use a   
   >> C11 print system using _Generic and variadic macros? I don't know   
   >> any. (I've written simple examples as proofs of concept, posted   
   >> in this group, but not for real use.) It turns out that people   
   >> /don't/ have to have workarounds. "printf" has its limitations -   
   >> there's no doubt there. But it is good enough for most people   
   >> and most uses.   
   >   
   > I recently played around with an attempted framework using _Generic.   
   > The goal was to be able to write something like   
   >   
   > print(s(x), s(y), s(z));   
   >   
   > where x, y, and z can be of more or less arbitrary types (integer,   
   > floating-point char*). The problem I ran into was that only one of   
   > the generic associations is evaluated (which one is determined at   
   > compile time), but *all* of them have to be valid code.   
      
   That is annoying but it shouldn't be too hard to work around   
   it. To verify that hypothesis I wrote this test case:   
      
      
    #include    
    #include    
    #include    
      
    #include "h/show.h"   
      
    int   
    main(){   
    unsigned long long ull = -1;   
    signed long long sll = -1;   
    unsigned long ul = -1;   
    signed long sl = -1;   
    unsigned char uc = -1;   
    signed char sc = -1;   
    unsigned short us = -1;   
    signed short ss = -1;   
    unsigned int ui = -1;   
    signed int si = -1;   
    char c = 'q';   
    float f = 1.23;   
    double d = 3.14159265358979312;   
    double long ld = 6.28318530717958623;   
      
    _Bool yes = 1;   
    _Bool no = !yes;   
      
    clock_t runtime = clock();   
    time_t now = time(0);   
    off_t offset = 27;   
    uint16_t u16 = -1;   
    int16_t s16 = -1;   
    uint_least32_t uge32 = -1;   
    int_least32_t sge32 = -1;   
    uint_fast32_t uf32 = -1;   
    int_fast32_t sf32 = -1;   
    char * foo = "foo";   
    const char * bas = "bas";   
      
    show(   
    uc,sc,us,ss,ui,si,ul,sl,ull,sll,   
    c,f,d,ld,yes,no,u16,s16,uge32,sge32,   
    runtime,now,offset,uf32,sf32,   
    c * now / 1e8 * ld,   
    foo, bas   
    );   
    printf( "\n" );   
      
    return 0;   
    }   
      
   which compiles under C11 and (along with the show.h include file)   
   produces output:   
      
    uc = 255   
    sc = -1   
    us = 65535   
    ss = -1   
    ui = 4294967295   
    si = -1   
    ul = 18446744073709551615   
    sl = -1   
    ull = 18446744073709551615   
    sll = -1   
    c = 'q'   
    f = 1.230000   
    d = 3.141593   
    ld = 6.283185   
    yes = true   
    no = false   
    u16 = 65535   
    s16 = -1   
    uge32 = 4294967295   
    sge32 = -1   
    runtime = 365   
    now = 1770371790   
    offset = 27   
    uf32 = 18446744073709551615   
    sf32 = -1   
    c * now / 1e8 * ld = 12569.638642   
    foo = "foo"   
    bas = (const char *) "bas"   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|