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,292 of 33,346   
   Marc to All   
   Re: overload resolution and function poi   
   14 May 12 15:30:16   
   
   From: marc.glisse@gmail.com   
      
   Daniel Krügler  wrote:   
   > Am 11.05.2012 21:21, schrieb Marc:   
   >> 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?   
   >   
   > I don't know how to realize your exact need, but consider the following   
   > as an alternative:   
   >   
   > // Examples:   
   > void f(int){}   
   > void f(double){}   
   > void f(int, bool){}   
   >   
   > #include    
   >   
   > template   
   > struct f_caller   
   > {   
   >     auto operator()(Args&&... args) const ->   
   > decltype(f(std::forward(args)...))   
   >     {   
   >       return f(std::forward(args)...);   
   >     }   
   > };   
   >   
   > If we consider this template as a kind of "holder" for some particular   
   > function overload f, what kind of property is missing for your use-case?   
   >   
   > Here an example of usage:   
   >   
   > template   
   > auto test(Args&&... args) -> f_caller   
   > {   
   >     return f_caller();   
   > }   
   >   
   > int main(){   
   >     test(0, true);   
   >     test(1.2);   
   >     test(2);   
   >     auto f2 = test(0, 0);   
   >     f2(0, 0);   
   > }   
   >   
   > There is a way of considering f2 as a function-pointer like storage of   
   > some unknown f.   
      
   That's a clever technique indeed. Not what I wanted for this question,   
   but I might find other uses for it :-)   
      
   > Since I don't know what your actual intention is, I leave that demonstration   
   > here - it might not what you want to realize.   
      
   The origin of this question is a discussion on the libstdc++ mailing   
   list about implementing the standard algorithms that use operator< in   
   terms of those that take a functor parameter (instead of duplicating   
   the code as is the case now). Perfect forwarding is not perfect, among   
   other things because it prevents copy elision, so passing the   
   algorithm a wrapper around operator< can make a difference. On the   
   other hand, if I could pass the algorithm a pointer to the appropriate   
   overload of operator<, the functor version could be equivalent to the   
   special-case version with operator<.   
      
   Then, because I am into theory, I wondered whether it was actually   
   possible to get the right pointer, and that question became more   
   interesting to me than the original issue.   
      
      
   --   
         [ 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