home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,433 of 33,346   
   Johannes Schaub to Marc   
   Re: extern "C" template   
   31 Aug 11 05:51:24   
   
   From: schaub.johannes@googlemail.com   
      
   Marc wrote:   
      
   > Let us look at a function that takes as argument a templated function   
   > type:   
   >   
   > struct A {   
   >          template   
   >          A( T(*f)(void) ) { f(); }   
   > };   
   >   
   > so I can construct an A from a function pointer, whatever the   
   > (ignored) return type. Now I'd like to also be able to construct A   
   > from extern "C" functions, but I can't seem to find a way. For a   
   > single signature, I'd use:   
   >   
   > extern "C" typedef X (*thetype)();   
   > struct A {   
   >          A(thetype f) { f(); }   
   > };   
   >   
   > but templates and extern "C" don't mix so well.   
   >   
   > Is it possible?   
   >   
      
   It is not possible to directly specify a function type with C language   
   linkage in C++11. But in the Bloomington meeting, it was deemed useful to   
   have 'extern "C"' linkage for alias templates possible. You can do the above   
      
   as follows then:   
      
       extern "C" {   
         template   
         using FCT = R(P...);   
       }   
      
   And formulate the function type nested in the function pointer type as   
   follows   
      
      struct A {   
         // note: T is still deducible   
         template   
         A(FCT *f) { f(); }   
      };   
      
   One can interpret the C++11 FDIS already in a way that makes this well-   
   formed, because language linkage is said to be not applied to templates.   
   However that interpretation isn't how the Standard is meant to be read -   
   language linkage is meant to *do* apply to templates, and makes the program   
   ill-formed if a C linkage is applied to templates. A post-C++0x draft   
   perhaps will explicitly allow applying language linkage to alias templates   
   (the main reason to disallow C linkage for templates has to do with extern   
   name manglings - not an issue for alias templates).   
      
   What you can try until compilers support the above is SFINAE tricks that   
   work independently of requiring you to supply a function type specifier. Or   
   tests such as the one based on that `void(X)` and `void(X*)` is the same for   
      
   a function type `X`. This would accept any-linkage function types I believe,   
      
   but if that does it for you, it's fine.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca