简体   繁体   中英

How to program this with variadic templates?

I have never used variadic templates myself, but think I could need them now. Suppose I have a class

class A {
  int Kern;
  template<int> void func_a(int, double) const;
  template<int> void func_b(double, double, char) const;
  template<int> unsigned func_c(float, std::vector<int> const&) const;
public
  /* ... */
  void FuncA(int, double) const;
  void FuncB(double, double, char) const;
  unsigned FuncC(float, std::vector<int> const&) const;
};

where the definitions of A::FuncA() etc. are all of the form

void A::FuncA(int i, double x) const
{
   switch(Kern) {
   case 1: return func_a<1>(i,x);
   case 2: return func_a<2>(i,x);
   case 3: return func_a<3>(i,x);
   /* ... */
   }
 }

I currently implement this switch with a C-macro

#define SwitchKernMacro(KERN,FUNC)   \
switch(KERN) {                       \
case 1: FUNC(1);                     \
case 2: FUNC(2);                     \
case 3: FUNC(3);                     \
/* ... */                            \
}

such that

void A::FuncA(int i, double x) const
{
#define FuncK(KERN) return func_a<KERN>(i,x);
  SwitchKernMacro(Kern,FuncK);
#undef FuncK
}

I like to avoid this C-macro in favour of a variadic template solution, such that the implementation of my functions becomes simply (or similar)

void A::FuncA(int i, double x) const
{ return SwitchKern(Kern,func_a,i,x); }    
void A::FuncB(double a, double b, char c) const
{ return SwitchKern(Kern,func_b,a,b,c); }
unsigned A::FuncC(float f, std::vector<int> const&v) const
{ return SwitchKern(Kern,func_c,f,v); }

How should the template SwitchKern look like?

EDIT

there seems to be some confusion about C++ templates and when they can be used. Suppose, I only have the following very simple functions

class A {
  int Kern;
  template int> void simple() const;
public:
  void Simple() const
  {
    switch(K) {
    case 1: return simple<1>();
    case 2: return simple<2>();
    case 3: return simple<3>();
    default: return simple<0>();
    }
  }
  /* ... */
};

then I can also implement A::Simple() via

class A {
  /* ... */
  template<int> friend struct simple_aux;
};

template<class T, template<int> class SimpleAux>
void Switch(int K, const T* a) {
  switch(K) {
  case 1: return SimpleAux<1>(a)();
  case 2: return SimpleAux<2>(a)();
  case 3: return SimpleAux<3>(a)();
  default: return SimpleAux<0>(a)();
  }
}

template<int k> struct simple_aux
{
  const A*const a;
  explicit simple_aux(const A*a__) : a(a__) {}
  void operator()() { return a->simple<k>(); }
};

void A::Simple() const
{ Switch<A,simple_aux>(K,this); }

However, this solution does not allow for return type different than void and for arbitrary arguments to the functions A::Simple() (passed to A::simple<>() ). My question was how to add these functionalities using variadic templates

The problem is that function templates can't be passed to a template, only class templates. You can work around this with helper classes:

template<template<int i> class Helper, typename... Args>
auto SwitchKern(int Kern, const A &a, Args &&...args)
-> decltype((a.*(Helper<0>::func()))(args...))
{
    switch (Kern) {
    case 1: return (a.*(Helper<1>::func()))(std::forward<Args>(args)...);
    case 2: return (a.*(Helper<2>::func()))(std::forward<Args>(args)...);
    case 3: return (a.*(Helper<3>::func()))(std::forward<Args>(args)...);
    }
}

template<int i>
struct FuncAHelper {
    static decltype(&A::func_a<i>) func() { return &A::func_a<i>; }
};

void A::FuncA(int i, double x) const
{
    return SwitchKern<FuncAHelper, int &, double &>(Kern, *this, i, x);
}

See also Is there a generic way to adapt a function template to be a polymorphic function object?

我认为模板都是关于编译时解决方案的,它们无法帮助解决运行时问题,因此如果您使用开关检查运行时值,则无法更改为模板(可变或正常)。

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