c++ - How to detect the presence and type of a member variable given its name? -



c++ - How to detect the presence and type of a member variable given its name? -

i know how write class can observe @ compile time if given class t has fellow member given name given type type, e.g.

#include <type_traits> template <typename t, typename type, bool = std::is_class<t>::value> struct has_member_foo { private: template <type t::*> struct helper; template <typename u> static std::false_type test(...); template <typename u> static std::true_type test(helper<&u::foo> *); typedef decltype(test<t>(nullptr)) testresult; public: static const bool value = testresult::value; }; template <typename t, typename type> struct has_member_foo<t, type, false> : std::false_type { }; struct has_foo { int foo; }; struct has_no_foo { int bar; }; void test() { static_assert(has_member_foo<has_foo, int>::value == true, ":("); static_assert(has_member_foo<has_no_foo, int>::value == false, ":("); static_assert(has_member_foo<int, int>::value == false, ":("); }

i don't have state exact type of fellow member variable because if want utilize these traits of time care whether fellow member convertible type, integral type, etc. , not exact type. i'd have ability observe presence , type of fellow member variable given name. able write this:

static_assert(has_member_foo<t>::value && std::is_integral<typename has_member_foo<t>::type>::value, "the type has have integral fellow member name foo");

if know build &t::foo legal, possible type of fellow member via like

template <typename t, typename u> t get_member_type(t u::*); typedef decltype(get_member_type(&t::foo)) the_member_type;

but cannot produce combination of 2 methods sfinaes right results, due helper-struct has know signature of pointer member. final code preprocessor macro name argument solutions allowed utilize preprocessor, too.

this simple way of doing variable named id:

#include <type_traits> using namespace std; template<typename t, typename v = bool> struct has_id : false_type { }; template<typename t> struct has_id<t, typename enable_if< !is_same<decltype(declval<t>().id), void>::value, bool >::type > : true_type { typedef decltype(declval<t>().id) type; };

and here how utilize it:

#include <iostream> using namespace std; struct x { int id; }; int main() { static_assert( has_id<x>::value && is_integral<has_id<x>::type>::value, "error!" ); }

you can create things simpler if can tolerate macros:

#define define_member_checker(member) \ template<typename t, typename v = bool> \ struct has_ ## fellow member : false_type { }; \ template<typename t> \ struct has_ ## member<t, \ typename enable_if< \ !is_same<decltype(declval<t>().member), void>::value, \ bool \ >::type \ > : true_type \ { \ typedef decltype(declval<t>().member) type; \ }; #define has_member(c, member) \ has_ ## member<c>::value #define member_type(c, member) \ has_ ## member<c>::type

you utilize them way:

define_member_checker(id) int main() { static_assert( has_member(x, id) && is_integral<member_type(x, id)>::value, "error!" ); }

c++ c++11 template-meta-programming

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -