相关疑难解决方法(0)

Variadic模板基类调用转发

在11 C++之前我有这样的事情:

template<class T,class U,class V>
struct Foo : T,U,V {

  bool init() {

    if(!T::init() || !U::init() || !V::init())
      return false;

    // do local init and return true/false
  }
};
Run Code Online (Sandbox Code Playgroud)

我想将其转换为C++ 11可变参数语法,以获得灵活长度参数列表的好处.我理解使用递归解压缩模板arg列表的概念,但我无法看到正确的语法.这是我尝试过的:

template<typename... Features>
struct Foo : Features... {

  template<typename F,typename... G>
  bool recinit(F& arg,G&& ...args) {

    if(!F::init())
      return false;

    return recinit<F,G...>(args...);
  }

  bool init() {
    // how to call recinit() from here?
  }
};
Run Code Online (Sandbox Code Playgroud)

我更喜欢对基类init()函数的调用顺序是从左到右,但它并不重要.

c++ templates variadic-templates c++11

8
推荐指数
1
解决办法
1807
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

variadic-templates ×1