C++ Function, take in enum and return a typedef class type to be used in template? -
is possible write (inline?) c++ function, accept enum input , returns class type can used in template declaration?
my intuition since there finite number of enum types, should possible?
enum myenumtype { a, b, c }; class myclassa { }; class myclassb { }; class myclassb { }; template class<t> class atemplatedclass { // ... }; notsurewhatreturntype convertenumtoclasstype(myenumtype type) { switch (type) { case a: return myclassa; case b: return myclassb; case c: return myclassc: default: throw; } } myenumtype type = gottensomewhere(); auto class_type = convertenumtoclasstype(type); atemplatedclass<class_type> is_this_possible;
functions cannot return types. need metafunction:
template <myenumtype> struct convertenumtoclasstype; template <> struct convertenumtoclasstype<a> { typedef myclassa type; }; template <> struct convertenumtoclasstype<b> { typedef myclassb type; }; // … etc. typedef convertenumtoclasstype<a> class_type; atemplatedclass<class_type> is_this_possible;
of course works @ compile time (since that’s when templates resolved).
Comments
Post a Comment