简体   繁体   中英

C++ variadic function templates

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?

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

eg:

#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');
}

您不能拥有模板化虚拟功能。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM