C++ 使用CRTP创建一个CRTP类构造函数,该构造函数接受基类和派生类的参数?

C++ 使用CRTP创建一个CRTP类构造函数,该构造函数接受基类和派生类的参数?,c++,templates,variadic-templates,template-meta-programming,crtp,C++,Templates,Variadic Templates,Template Meta Programming,Crtp,我遇到了以下困境,在我的代码中,有三个类遵循一种模式,其中某些函数和构造函数在所有三个类中都是相同的。为了解决这个问题,我创建了这样一个类 template<class Derived> class BaseCRTP : public Base{ // example of what I'm extracting into this. BaseCRTP(const BaseCRTP<Derived>& rhs){ ... } //other functions

我遇到了以下困境,在我的代码中,有三个类遵循一种模式,其中某些函数和构造函数在所有三个类中都是相同的。为了解决这个问题,我创建了这样一个类

template<class Derived>
class BaseCRTP : public Base{

// example of what I'm extracting into this. 
BaseCRTP(const BaseCRTP<Derived>& rhs){
...
}
//other functions
...

}
其中,对于每个派生类型,DerivedParamTypeN的类型和数量都会发生变化

我想看看是否可以在CRTP中创建与以下模式相同的构造函数:

BaseCRTP(BaseParamType1 param1 BaseParamType2 param2 DerivedParameters...): Base(param1, param2){
    static_cast<Derived*>(this)->setMembers(derivedParameters)
}
BaseCRTP(BaseParamType1 param1 BaseParamType2 param2派生参数…):Base(param1,param2){
静态_cast(this)->setMembers(derivedParameters)
}
我一直在研究和之类的问题,但我不确定这些是否是解决我的问题的最佳解决方案,我正在考虑做一些CRTP采用两个模板参数的事情,如:

template<class Derived, typename ... DerivedParams>
class BaseCRTP : public Base{
...
}
模板
类BaseCRTP:公共基{
...
}
与:

派生类:公共BaseCRTP{
公众:
使用公共BaseCRTP::BaseCRTP
...
}
以及:

BaseCRTP(BaseParamType1 param1 BaseParamType2 param2 const DerivedParameters&…DerivedParameters):Base(param1,param2){
静态_cast(此)->setMembers(派生参数…)
}

对于这个用例,是否有替代方案,或者更好的答案?

BaseCRTP
构造函数中将
this
转换为
Derived
是错误的,因为此时还没有构造派生类。@VTT似乎是这样。注意,我说的是在
BaseCRTP
构造函数中转换,不是关于在某些成员函数中强制转换。@VTT那么替代方案是什么?替代方案?让派生类成为模板而不是CRTP怎么样?
template<class Derived, typename ... DerivedParams>
class BaseCRTP : public Base{
...
}
class Derived : public BaseCRTP<Derived, DerivedParamType1, DerivedParamType2, DerivedParamType3 ...>{
    public:
        using public BaseCRTP<Derived, DerivedParamType1, DerivedParamType2, DerivedParamType3 ...>:: BaseCRTP<Derived, DerivedParamType1, DerivedParamType2, DerivedParamType3 ...>
...
}
BaseCRTP(BaseParamType1 param1 BaseParamType2 param2 const DerivedParameters&... derivedParameters): Base(param1, param2){
    static_cast<Derived*>(this)->setMembers(derivedParameters...)
}