I ran into the following predicament, in my code I have three classes that follow a pattern where certain functions and constructors are identical across all three. In order to solve this problem, I created a CRTP class like so.
template<class Derived>
class BaseCRTP : public Base{
// example of what I'm extracting into this.
BaseCRTP(const BaseCRTP<Derived>& rhs){
...
}
//other functions
...
}
One of the functions I wanted to extract into the CRTP I realized did not have the same function signature but followed a pattern none the less, this function looked like this:
//before CRTP
Derived( BaseParamType1 param1 BaseParamType2 param2 DerivedParamType1 param3 ...) : Base(param1, param2) {
setMembers(param3, param4...)
}
where for each derived type, the types and number of DerivedParamTypeN would change.
I wanted to see if I could create the same constructor in the CRTP that followed the pattern as follows:
BaseCRTP(BaseParamType1 param1 BaseParamType2 param2 DerivedParameters...): Base(param1, param2){
static_cast<Derived*>(this)->setMembers(derivedParameters)
}
I've been looking into things like parameter packs and variadic templates, but I'm not sure if those are the best solution for my problem, I was thinking of maybe doing something where the CRTP takes two template parameters like so:
template<class Derived, typename ... DerivedParams>
class BaseCRTP : public Base{
...
}
with:
class Derived : public BaseCRTP<Derived, DerivedParamType1, DerivedParamType2, DerivedParamType3 ...>{
public:
using public BaseCRTP<Derived, DerivedParamType1, DerivedParamType2, DerivedParamType3 ...>:: BaseCRTP<Derived, DerivedParamType1, DerivedParamType2, DerivedParamType3 ...>
...
}
and:
BaseCRTP(BaseParamType1 param1 BaseParamType2 param2 const DerivedParameters&... derivedParameters): Base(param1, param2){
static_cast<Derived*>(this)->setMembers(derivedParameters...)
}
Are there alternatives, or better formed answers for this use case?