Template type member variables in c++ -
i'm trying change type of membervariable, based on whats written within template.
for example a<64, 64>
should make member int_128 sadly, have no idea how work template types, , every tutorial find helps template functions.
my class looks like
template<int x, int y> class a{ private: typetobegeneric m_variable }
is there way in constructor like
if( x+y <= 64){ typetobegeneric = int_64 } else{typetobegeneric = int_128}
i don't want add specific type within template<>. structure a<64, 64> should untouched.
constexpr bool lessthan64(int a,int b) { return (a+b) < 64; } template<int x, int y> class a{ using type = typename std::conditional<lessthan64(x,y),int_64,int_128>::type; private: type m_variable; }
using constexpr functions compile time metaprogramming can evaluate values @ compile time , use std::conditional chose between 2 types.
edit: more 2 types can use variadic templates or explicit specialization.
template<unsigned int i,typename... sizes> struct select; template<unsigned int i,typename t,typename... sizes> struct select<i,t,sizes...>:select<n-1,cases...> { } template<unsigned int i,typename t,typename... sizes> struct select<o,t,sizes...> { using type =t } need constexpr function differentiate between types.
Comments
Post a Comment