Thursday 15 July 2010

c++ - Check template argument of Base class against values in Derived -



c++ - Check template argument of Base class against values in Derived -

lets assume have interface class (made-up example, not real code)

template <int year> class auto { public: virtual void move(double x, double y) = 0; // etc etc };

and lot of derived classes like

template <int year> class model8556 : virtual public car<year> { private: void move(double x, double y) { // ... } int yearmax = 2000; // different every model int yearmin = 1990; // etc etc };

and take model somewhere via

car<foo>* mycar; switch (bar) { case 1: mycar = new model3434<foo>(); break; case 2: mycar = new model8295<foo>(); break; // etc }

i want check template argument of auto (or better: of derived classes) @ compile time. want template argument year remain in range (i.e. between yearmin , yearmax). however: range differs between derived classes. (edit:) as there lot of derived classes, prefer solution within car.

how accomplish behaviour? or bad design?

any help appreciated.

do mean this?

template <int year> class model8556 : virtual public car<year> { private: static const int yearmax = 2000; // assume meant static constant static const int yearmin = 1990; static_assert( yearmin <= year && year <= yearmax, // status "invalid template argument specified!" ); // error message };

demo. there no possibility set base of operations class current method; crtp doesn't work because derived class considered incomplete within car. however, alter in construction might help.

template <int year> class auto { // implementation, above }; template <int year, int yearmin, int yearmax> class carchecker : car<year> { // optionally declare constants here static_assert( yearmin <= year && year <= yearmax, "invalid template argument specified!" ); }; template <int year> class model8556 : public carchecker<year, 1990, 2000> // specify minimum , maximum here {};

c++ inheritance interface

No comments:

Post a Comment