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,894 of 33,346    |
|    Ulrich Eckhardt to All    |
|    Re: currying pointer to member functions    |
|    07 Mar 13 00:27:10    |
   
   d4526246   
   From: ulrich.eckhardt@dominolaser.com   
      
   Am 06.03.2013 00:20, schrieb Jerry:   
   > I appreciate any advice about how to do this.   
   >   
   > I can make this work:   
   >   
   > struct a   
   > {   
   > int b;   
   > int c() {return b+1;}   
   > int d(int x) {return b+x;}   
   > };   
   >   
   > int main()   
   > {   
   > a m = { 1 }, n = { 2 };   
   > a *ps = &m;   
   > int (a::*pf)() = &a::c;   
   > std::cout << (ps->*pf)() << std::endl;   
   > return 0;   
   > }   
   >   
   > And it runs the function and everything works. But what I want to   
   > do is curry the function so that I can store (ps->*pf) and then   
   > later execute it. So what is the type of &(ps->*pf) ?   
      
   I'd rephrase that: What is the type of "ps->*pf"? Also similar, what   
   is they type of "m.d"? In either case, I'm afraid that there is no C++   
   type associated with this, so you also can't store this anywhere.   
      
   It's a bit like taking the address of an overloaded function, which   
   you can do but which requires you to static_cast it to one of the   
   overloads to disambiguate, because it doesn't have a type or value   
   itself otherwise.   
      
   I can only guess what you're trying to do, but I guess you want   
   something similar to Python's bound memberfunctions. The expression   
   "some_object.some_function" creates such a bound memberfunction that   
   carries information about both the object and function to call. This   
   bound memberfunction can then be called like any other function:   
      
    # variant 1   
    some_object.some_function(42)   
    # variant 2   
    bmf = some_object.some_function   
    bmf(42)   
      
   For that, modern C++ provides function objects and utilities to bind   
   parameters. These initially sprung from Boost, which is also what I   
   would look at in your case. The code then looks like this:   
      
    a m = {1};   
    // create a functor that takes and returns an int   
    function
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca