Variadic CRTP template class with a field using constexpr basing on the parameter classes list

  • A+

I've written (in c++11) a variadic template constexpr function which calculates max sizeof of the parameter types, e.g.:

maxSizeof<int, char, MyType>() 

That works correctly. Then I would like to have a variadic template class with a field which is an array of size equal to the maxSizeof(). That should also work correctly:

template <typename... TypesT>     class Myclass {         uint8_t field[maxSizeOf<TypesT...>()]     } 

But I also need Myclass to declare methods for each of the parameter types. I've used CRTP for that in following way:

template <typename... TypesT> class Myclass;  template <> class Myclass {     uint8_t field[maxSizeOf<TypesT...>()] // (1) Couldn't do it here as there are no `TypesT` }  template <typename FirstT, typename... OtherT> class Myclass<FirstT, OtherT...> : public Myclass<OtherT...> {     public:         virtual void doSomething(FirstT object) = 0;     private:         uint8_t field[maxSizeOf<FirstT, OtherT...>()] // (2) Shouldn't do it here as it will create field for all of the "middle" classes } 

The question is how implement the declarations of the methods and in the same time have the array field with proper size. (1) and (2) doesn't work for the reasons stated in comments.


Like most SW engineering problems, this can be solved by adding more layers of indirection[1]:

template <typename... TypesT> class MyclassFunctions;  template <> class MyclassFunctions {};  template <typename FirstT, typename... OtherT> class MyclassFunctions<FirstT, OtherT> : public MyClassFunctions<OtherT...> { public:   virtual void doSomething(FirstT object) = 0; };  template <typename... TypesT> class Myclass : public MyclassFunctions<TypesT...> {   uint8_t field[maxSizeOf<TypesT...>()] }; 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: