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,001 of 33,346   
   Johannes Schaub to All   
   Re: Variadic templates and passing by re   
   11 Mar 12 18:49:01   
   
   From: schaub.johannes@googlemail.com   
      
   Am 11.03.2012 08:01, schrieb Thomas Richter:   
   > 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.   
      
   Use rvalue references and the deduction hack (which deduces U to "X&" if   
   the argument is an lvalue, and to "X" if the argument is an rvalue. X be   
   the argument type passed to Launch - in this case, "int" and "A").   
      
    template   
    void Launch(A &t, U &&u, Us &&...other) {   
     u = ...; // now works!   
    }   
      
      
   That works because the name of an rvalue reference is always an lvalue.   
   I don't know what the "t" parameter in your function is for, but I'm   
   assuming you don't want to write to it. Neither do I know what your   
   other "int" Launch overload was for.   
      
      
   --   
         [ 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