问题描述
从www.gstatic.com考虑情况,主要是
template <typename T,typename ...Args>
class ChildGenerator : public Args...
{
public:
ChildGenerator(T t) : Args(t)... {}
void doThings() override { (static_cast<void>(Args::doThings()),...);}
};
有一点不同的是,继承现在看起来更像 A<Args>...
,即
template<typename> class A{some code};
template <typename T,typename ...Args>
class ChildGenerator : public A<Args>...
{
public:
ChildGenerator(T t) : A<Args>(t)... {}
void doThings() override { (static_cast<void>(A<Args>::doThings()),...);}
};
到目前为止,一切看起来都很好很优雅,甚至可以工作,但实际感兴趣的 class A
有更长的模板列表,例如
template <typename T1,typename T2,...,typename Tn,typename ...Args>
class ChildGenerator : public A<T1,T2,Tn,Args>...
这会使代码变得笨拙,例如
void doThings() override { (static_cast<void>(A<T1,Args>::doThings()),...);}
特别是当有多个 doThings
函数时。
typedef
多个基数的一种方法是使用元组:
typedef std::tuple<A<T1,Args>...> Bases;
不过
void doThings() override { (static_cast<void>(A<Bases>::doThings()),...);}
不会编译。问题是有没有办法让这个工作。
我目前找到的替代解决方案
-
因为可以调用单个
doThings()
,就像在static_cast( ( tuple_element::type::doThings()) );
因此 index_sequence
会成功,虽然稍微不那么优雅。
- 更优雅的选择是
template < typename Arg >
using Bases = A<T1,Arg>;
然后
(static_cast<void>(Bases<Args>::doThings()),...);
足够优雅,是目前最好的选择。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)