3

The concept of variadic templates is quite confusing to me and I want to make it a bit more complex (well I think...).
Let us consider the following code:

template <typename T>
class base  
{  
    template <typename... E>  
    virtual void variadic_method_here(E... args) = 0;  
};

and an implementing class:

class derive : public base<some_object> 
{  
    void variadic_method_here(concrete_args_here);  
};

How do I do that?

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
Royi Freifeld
  • 629
  • 2
  • 11
  • 31

2 Answers2

4

I think if I were faced with this problem I'd use CRTP and overloads to solve the problem.

e.g.:

#include <iostream>

template <typename Impl>
class base {
public:
   template <typename... E>
   void foo(E... args) {
      Impl::foo_real(args...);
   }
};

class derived : public base<derived> {
public:
   static void foo_real(double, double) {
     std::cout << "Two doubles" << std::endl;
   }

   static void foo_real(char) {
     std::cout << "Char" << std::endl;
   }
};

int main() {
  derived bar;
  bar.foo(1.0,1.0);
  bar.foo('h');
}
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • 1
    Which still leaves the problem of runtime-polymorphism OP is apparently aiming for. – pmr Oct 02 '11 at 13:43
  • 3
    @pmr - They don't specifically ask for runtime polymorphism - to me it seemed like they wanted compile time polymorphism but didn't know about CRTP – Flexo Oct 02 '11 at 13:44
  • 1
    You could be right. The `virtual` in OPs code could be a not-so-concious decision. – pmr Oct 02 '11 at 13:47
  • @awoodland - You're right. I never heard about CRTP and it sounds like a good idea, but I'm still not sure it will solve my problem. The derived class might be decided by some clicks on GUI of my application. Will it work if I have 2 or more derived classes? – Royi Freifeld Oct 02 '11 at 14:16
  • @RoyiFreifeld - sounds like you want runtime polymorphism then if there's more than 1 derived class in use in your application. The problem with `template virtual` functions is how does the compiler decide which types to instantiate for? There's no reason to assume that where the compiler sees the call and where it's implemented are in the same translation unit, so figuring out which variants to instantiate in derived classes is (in general) impossible for the compiler. – Flexo Oct 02 '11 at 14:37
  • @awoodland - let me try explaining it a little bit better: I have a single base pointer. The construction will happen on runtime, but there's still only one. Like a Strategy/State design pattern if you like. That's why I used the virtual keyword. – Royi Freifeld Oct 02 '11 at 14:54
4

You can't have a templated virtual function.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    Anonymous downvoter: please always indicate why you downvote. I have to guess that you thought (erroneously) that this didn't answer the OP's question. It is certainly correct, and it concerns directly the OP's code. One reason I have mostly quit helping folks on SO is that this this place evolved into being the #1 Herb Schildt site on the net. And one main reason for that, IMHO, was willy-nilly and/or social downvotes like yours. Tecnical discussion isn't democracy. Votes do not trump logic or facts -- except on SO. – Cheers and hth. - Alf Dec 04 '13 at 22:50