I tried to find a way to eliminate this code (at compile time) (since two days :-) β get_value is depressing.
#include <iostream> template <typename T> struct type2type {}; template<class T, int val> struct BASE { static constexpr int get_value ( type2type< T > ) { return val; } }; class X {}; class Y {}; struct A : public BASE< X, 1 >, public BASE< Y, 0 > {}; int main ( int argc, char **argv ) { A a {}; std::cout << a.get_value ( type2type< X >{} ) << std::endl; }
This is the working time.
#include <iostream> template <typename T> struct type2type {}; template<class T> struct VIRTUAL { int get_value () const { return get_value_from_BASE ( type2type< T > {} ); } private: virtual int get_value_from_BASE ( type2type< T > ) const = 0; }; template<class T, int val> class BASE : public VIRTUAL< T > { virtual int get_value_from_BASE ( type2type< T > ) const override { return val; } }; class X {}; class Y {}; struct A : public BASE< X, 1 >, public BASE< Y, 0 > {}; int main ( int argc, char **argv ) { A a {}; std::cout << a.::VIRTUAL< X >::get_value () << std::endl; }
Is there a solution?
Note. The possible way I found is above std :: is_base_of <>, but it is very small (depth of the template instance)
source share