C++ templates: how to determine if a type is suitable for subclassing -
let's have templated class depending on type t
. t
anything: int
, int*
, pair <int, int>
or struct lol
; cannot void
, reference or cv-qualified though. optimization need know if can subclass t
. so, i'd need trait type is_subclassable
, determined logical combination of basic traits or through sfinae tricks.
in original example, int
, int*
not subclassable, while pair <int, int>
, struct lol
are.
edit: litb pointed out below, unions not subclassable , t
can union type well.
how write trait type need?
you want determine whether non-union class. there no way known me (and boost hasn't found way either). if can live union cases false positives, can use is_class
.
template<typename> struct void_ { typedef void type; }; template<typename t, typename = void> struct is_class { static bool const value = false; }; template<typename t> struct is_class<t, typename void_<int t::*>::type> { static bool const value = true; };
boost has is_union
uses compiler-specific builtins though, here. is_class
(which boost provides) combined is_union
solve problem.
Comments
Post a Comment