Variadic template functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dshin
    New Member
    • Sep 2008
    • 5

    Variadic template functions

    Is it possible to create a a variadic template function? Something like this:

    Code:
    class Foo {
    	...
    	Foo(int x);
    	...
    };
    
    class Bar {
    	...
    	Bar(int x, double y);
    	...
    };
    
    template<typename T> class Template {
    	...
    	static void method(...) {
    		T t(...);  // variadic call
    		...
    	}
    };
    
    int main() {
    	Template<Foo>::method(5);
    	Template<Bar>::method(5, 0.3);
    }
    I don't want to make Foo and Bar have variadic constructors.
  • dshin
    New Member
    • Sep 2008
    • 5

    #2
    I realized my use of "variadic" is a little misleading here - the underlying methods themselves are not variadic.

    Basically, I want Template::metho d() to pass its arguments on to an appropriate constructor. The variadicity comes from the fact that the different constructors do not share the same signature.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by dshin
      Basically, I want Template::metho d() to pass its arguments on to an appropriate constructor. The variadicity comes from the fact that the different constructors do not share the same signature.
      The two templates are different in their content. Here is where you use the template for, say, all constructors with one argument and then write an explicit specialization( s) for the type(s) that have more than one constrcutor argument. To the user it will appear there is one template. What goes on behind the scenes is largely irrelevant.

      Comment

      • dshin
        New Member
        • Sep 2008
        • 5

        #4
        Originally posted by weaknessforcats
        The two templates are different in their content. Here is where you use the template for, say, all constructors with one argument and then write an explicit specialization( s) for the type(s) that have more than one constrcutor argument. To the user it will appear there is one template. What goes on behind the scenes is largely irrelevant.
        It seems to me then that I'd need the specialized template to rewrite the entire class Template<Bar>, just to add the one extra method, which is not nice.

        So that gets me thinking that I'd need to create a SpecializedTemp late<Bar> class to inherit from Template<Bar>, in order to add the extra method() without rewriting the rest of Template<T>.

        Is that what you are suggesting?

        Comment

        Working...