在 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
}
};
我想将其转换为 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?
}
};
我希望基类 init() 函数的调用顺序是从左到右的,但这并不重要。