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 32,288 of 33,346   
   Wil Evers to Pete Becker   
   Re: overload resolution and function poi   
   12 May 12 21:42:02   
   
   From: bouncer@dev.null   
      
   Pete Becker wrote:   
      
   > On 2012-05-11 19:21:54 +0000, Marc said:   
   >   
   >> Hello,   
   >>   
   >> I have several overloads of a function, I have a set of arguments,   
   >> and I would like to get a function pointer to the overload the   
   >> compiler would pick if I called this function on these arguments. I   
   >> am not interested in applying the function now, I just want to get   
   >> that pointer.   
   >>   
   >> Is this possible? Or is anything vaguely similar possible?   
   >   
   > Use a cast to select from a set of overloaded functions:   
   >   
   > void f(int);   
   > void f(double);   
   >   
   > typedef void (*f_of_double)(double);   
   >   
   > f_of_double fptr = (f_of_double)&f;   
   >   
   > The typedef isn't actually needed; I used it here to clarify what's   
   > going on. Yes, the cast looks wierd, but that's the rule.   
      
   I don't think that cast is required.  This should also work:   
      
     void f(int);   
     void f(double);   
      
     typedef void (*f_of_double)(double);   
      
     f_of_double fptr = &f;   
      
   Unnecessary casts, especially C-style casts, should be avoided:   
   they tell the compiler to be silent about potentially dangerous   
   conversions.   
      
   - Wil   
      
      
   --   
         [ 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