27 May 2011

C++0x: member_of template

template<typename F>
struct member_of
{
private:
//template <class T, typename R, typename... Args> static typename T* __fn_to_cx(R(T::*)(Args...));
template <class T, typename R> static typename T __fn_to_cx(R(T::*)());
template <class T, typename R, typename A1> static typename T __fn_to_cx(R(T::*)(A1));
template <class T, typename R, typename A1, typename A2> static typename T __fn_to_cx(R(T::*)(A1,A2));
template <class T, typename R, typename A1, typename A2, typename A3> static typename T __fn_to_cx(R(T::*)(A1,A2,A3));
template <class T, typename R, typename A1, typename A2, typename A3, typename A4> static typename T __fn_to_cx(R(T::*)(A1,A2,A3,A4));
template <class T, typename R, typename A1, typename A2, typename A3, typename A4, typename A5> static typename T __fn_to_cx(R(T::*)(A1,A2,A3,A4,A5));
public:
typedef typename decltype(__fn_to_cx(F())) type;
};

Usage: member_of<&SomeClass::SomeMember>::type will always be of type SomeClass.

That is all.
Post a Comment