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,002 of 33,346   
   Marc to Thomas Richter   
   Re: Variadic templates and passing by re   
   11 Mar 12 18:49:14   
   
   From: marc.glisse@gmail.com   
      
   Thomas Richter  wrote:   
      
   > Hi folks,   
   >   
   > how would you realize a variadic template function where you want to   
   > pass in both scalar types and references you want to modify in the   
   > callee. To be specific, consider declarations as follows:   
   >   
   >   class A;   
   >   
   >   template   
   >   void Launch(A &t,U&... other);   
   >   
   >   template   
   >   void Launch(int x,U&... other);   
   >   
   > Now I get a problem because, when I want to call "Launch" with int   
   > arguments as in   
   >   
   >  class A a,b;   
   >  Launch(a,5,b);   
   >   
   > it clearly doesn't work. This would require the compiler to create a   
   > reference to a temporary, which is of course not possible. Replacing the   
   > arguments by const references would solve the problem as it would allow   
   > the compiler to pass in references to temporaries. However, this   
   > prohibits of course modification of the classes passed in, which I would   
   > like to do. Replacing the reference by a copy would of course also solve   
   > the problem of argument passing, but then I only modify the copy, so   
   > this doesn't help either.   
   >   
   > Any other ideas?   
      
     template   
     void Launch(U&&...);   
      
   This will accept an int. And when you pass it an lvalue reference, it   
   will deduce U as A& and thus U&& will also be A& and it will do what   
   you want.   
      
   You may want to take a look at some "perfect forwarding" article, the   
   idea they explain is the same.   
      
      
   --   
         [ 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