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,978 of 33,346   
   Marc to Thomas Richter   
   Re: Trick for many identical arguments   
   03 Mar 12 01:03:47   
   
   From: marc.glisse@gmail.com   
      
   Thomas Richter  wrote:   
      
   > is there a nice (template ?) trick to generate functions with variable   
   > number of arguments without using varargs (and hence, no type-checks)?   
   > The use case would be to generate functions for   
   >   
   > class Argument;   
   >   
   > class Caller {   
   > 	void Launch(Argument arg1);   
   > 	void Launch(Argument arg1,Argument arg2);   
   > 	void Launch(Argument arg1,Argument arg2,Argument arg3);   
   > 	// and so on   
   > };   
   >   
   > without the code repetition as in the above case. Arguments are all   
   > identical in type, but their number may grow very large and I don't want   
   > to repeat the same type of code all over again.   
      
   The easiest is probably to use the boost preprocessor library.   
      
   In C++11, you can you variadic templates. Note that it won't give you   
   the equivalent of   
   	void Launch(Argument arg1);   
   	void Launch(Argument arg1,Argument arg2);   
   	void Launch(Argument arg1,Argument arg2,Argument arg3);   
   but of:   
   	template void Launch(T arg1);   
   	template void Launch(T arg1,U arg2);   
   	template void Launch(T arg1,U arg2,V arg3);   
      
   you can use sfinae to constrain the arguments to be convertible to   
   Argument. You can also make Launch convert its arguments to Argument   
   and forward them to a function that contains the real code, to avoid   
   binary code duplication.   
      
      
   --   
         [ 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