suppose have several structs fields optional:
struct param1 { static const bool x = true; }; struct param2 { };
and want write template function
template <class paramtype> bool returnparam();
which should return paramtype::x
if static field x
exists, otherwise should return false
i suppose sfinae magic should help.
constraint
i using msvc 2010, cannot use of c++11 features.
this code works perfectly:
template<typename t, typename memberptrgetter> struct has_member_impl { private: template<typename u> static char test(typename memberptrgetter::template type<u>*); template<typename u> static int test(...); public: static const bool value = sizeof(test<t>(0)) == sizeof(char); }; template <typename t, typename memberptrgetter> struct has_member: public std::integral_constant<bool, has_member_impl<t, memberptrgetter>::value> { }; struct staticmembergetter { template <typename t, const bool = t::x> struct type { }; }; struct param1 { static const bool x = true; }; struct param2 { }; template <typename param, typename t = void> struct extractparam { static const bool x = false; }; template <typename param> struct extractparam<param, std::true_type> { static const bool x = param::x; }; template <typename param> bool returnparam() { return extractparam<param, has_member<param, staticmembergetter>>::x; }
Comments
Post a Comment