我正在尝试创建一个从多个类(由可变参数模板定义)继承的类,并且对于每个类,将相同的 args 参数包传递给每个类的构造函数。但是,似乎我无法解压缩类的可变参数模板和 args 的参数包。
我有一堂课:
template<class... __Policies>
class GenericPolicyAdapter : public __Policies...{
使用构造函数:
template<class... __Args>
GenericPolicyAdapter( __Args... args ) : __Policies( args... ){
并测试:
GenericPolicyAdapter<T1,T2> generic_policy_adapter( arg1, arg2, arg3 );
gcc 失败:
error: type ‘__Policies’ is not a direct base of ‘GenericPolicyAdapter<T1,T2>’
在哪里__Policies = T1, T2
为了澄清,我本质上是在尝试做:
GenericPolicyAdapter : public T1, public T2
{
public:
template<class... __Args>
GenericPolicyAdapter( __Args... args ) : T1( args... ), T2( args... ){}
};
但从T1
_ T2
___Policies
有任何想法吗?似乎 gcc 将__Policies
其视为单一类型而不是类型列表。提前致谢!
编辑:
我应该澄清一下我使用的是 gcc/g++ 4.4.5。
Howard Hinnant 的建议是:
template<class... __Args>
GenericPolicyAdapter( __Args... args )
: __Policies( args...)...
{}
但是,对于 gcc/g++ 4.4.5,这给出了invalid use of pack expansion expression
. 这在 OSX/clang 中工作很好,但是有没有办法在 gcc/g++ 中做到这一点?