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,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 fn = bind(&a::c, &m);   
         // calls m.c(42)   
         fn(42);   
      
      
   > Oh, and to make all this more interesting (i.e. complicated) I am   
   > working with a compiler that is more than 10 years old and is only   
   > compatible with C++98 - if I could a newer compiler I wouldn't be   
   > asking this!   
      
   Which one? This might be important in order to provide good   
   suggestions.  Chances are that even if the latest Boost version   
   doesn't support your compiler, you might have luck with an older   
   version.   
      
      
   Greetings from Hamburg!   
      
   Uli   
      
      
   --   
         [ 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