c++ - 如何使用可变参数模板对此进行编程?

标签 c++ c++11 variadic-templates

我自己从未使用过可变参数模板,但我认为我现在可能需要它们。假设我有一个类

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;
};

其中 A::FuncA() 的定义等等都是形式

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);
   /* ... */
   }
 }

我目前使用 C 宏实现此开关

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

这样

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

我喜欢避免使用这个 C 宏,转而使用可变参数模板解决方案,这样我的函数的实现就变得简单(或类似)

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

模板应该怎么弄SwitchKern看起来像?

编辑

似乎对 C++ 模板以及何时可以使用它们有些困惑。假设,我只有以下非常简单的功能

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>();
    }
  }
  /* ... */
};

那我也可以实现A::Simple()通过

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

但是,此解决方案不允许返回类型不同于 void以及函数的任意参数 A::Simple() (传递给 A::simple<>() )。我的问题是如何使用可变参数模板添加这些功能

最佳答案

问题是函数模板不能传递给模板,只能传递给类模板。您可以使用辅助类解决此问题:

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

另见 Is there a generic way to adapt a function template to be a polymorphic function object?

关于c++ - 如何使用可变参数模板对此进行编程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12586829/

相关文章:

c++ - 如何在 OpenCV 中使用 gpu::Stream?

c++ - 如何比较字符串数组中的单词?

c++ - 为什么这个 constexpr 函数格式错误?

c++ - 如何创建函数模板来扩展不代表函数参数的参数包?

c++ - 可变参数结构体臃肿,在结构体末尾添加了额外的填充

c++ - 在 OpenGL 中使用 std::vector 作为顶点/元素列表

c++ - 如何在 C++ 中的 vector 结构中使用结构 vector ?

c++ - 使用绑定(bind)与使用带有随机数生成器的指针

c++ - 如何在不知道C++中有多少个可选参数的情况下在循环中使用va_arg?

c++ - 基派生类关系