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,996 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Haskell-style "dot" operator   
   08 Mar 12 16:46:19   
   
   50b4b57b   
   From: daniel.kruegler@googlemail.com   
      
   Am 08.03.2012 21:44, schrieb Quint Rankid:   
   > On Mar 6, 4:18 pm, "Jimmy H."  wrote:   
   >   
   >> #include   
   >> #include   
   >>   
   >> template   
   >> auto operator*(std::function  a, std::function  b) ->   
   >>      std::function   
   >> {   
   >>      return [a,b](C c) { return a(b(c)); };   
   >>   
   >> }   
   >>   
   >> int addOne(int a) {   
   >>      return a + 1;   
   >>   
   >> }   
   >>   
   >> int main() {   
   >>      std::function  addOneFunc(addOne);   
   >>      std::cout<<  (addOneFunc * addOneFunc)(2)<<  std::endl;   
   >>   
   >> }   
   >>   
   >> Notably, though, I need to explicitly convert addOne to a   
   >> std::function for it to call the custom operator* function. Attempting   
   >> to write a helper template to do the conversion for me:   
   >   
   > I don't understand.   
   >   
   > The above code seems to compile and run using GNU C++ (GCC) version   
   > 4.6.1 (mingw32).  I used the -std=c++0x command line argument.   
   >   
   > I think that you are using a compiler that doesn't support this, or I   
   > don't understand what you are trying to accomplish.  Can you please   
   > say what compiler you are using and expand on the need to explicitly   
   > convert addOne.   
      
   No, you are misunderstanding the OP here (I also needed a while to   
   understand the request): He described that above construction works, his   
   complain was that instead of above shown expression   
      
   (addOneFunc * addOneFunc)(2)   
      
   the simpler one   
      
   (addOneFunc * addOne)(2)   
      
   using the function addOne directly as operand does not compile. This is   
   so, because argument conversions are (usually) not considered for   
   function templates. One of the rare exceptions are function-to-pointer   
   and array-to-pointer conversions when the template parameter is not a   
   reference type (see [temp.deduct.call] p2).   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
         [ 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