0

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?

Krupip
  • 4,404
  • 2
  • 32
  • 54
  • It is wrong to cast `this` to `Derived` in `BaseCRTP` constructor because derived class is not yet constructed at this point. – user7860670 Jun 15 '17 at 18:58
  • @VTT it seems to follow [the second example given on Wikipedia](https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern). – Krupip Jun 15 '17 at 19:01
  • Notice that I'm talking about casting in `BaseCRTP` constructor, not about casting in some member function. – user7860670 Jun 15 '17 at 19:03
  • @VTT so what is the alternative? – Krupip Jun 15 '17 at 19:09
  • Alternative? How about making derived class a template instead of CRTP? – user7860670 Jun 15 '17 at 19:18
  • @VTT how does that work in my problem? its the constructors which have code duplication, I would need to template on my own class in order to get rid of that duplication, unless I could create the member variables in the template itself (in which case i still need to inherit from another templated class) – Krupip Jun 15 '17 at 19:20
  • Do you know that you can [inherit constructor from base class](https://stackoverflow.com/questions/347358/inheriting-constructors)? – user7860670 Jun 15 '17 at 19:24
  • @VTT, I know, I edited the post to have that, that still doesn't change anything. – Krupip Jun 15 '17 at 19:26

0 Answers0