You can make a "T" to provide this information.
template <class ...T> struct BoundTypes { }; template <class U, class T> struct BinderDecls { void AddMatch(); }; template <class U, class T, class ...Ts> struct BinderDecls<U, BoundTypes<T, Ts...>> :BinderDecls<U, BoundTypes<Ts...>> { using BinderDecls<U, BoundTypes<Ts...>>::AddMatch; template<TU::*PtrTomember> void AddMatch(); }; template <class T> struct Binder : BinderDecls<T, typename T::bound_types> { }
Then it becomes easy
struct TestType { typedef BoundTypes<int, float> bound_types; int i; float j; }; int main(int argc, char** argv) { Binder<TestType> b; b.AddMatch<&TestType::i>(); b.AddMatch<&TestType::j>(); }
Alternatively, you can use friend function definitions
template <class ...T> struct BoundTypes { }; template <class U, class T> struct BinderDecls { template<TU::*ptr> friend void addMatch(BinderDecl &u) { // ... } }; template <class U, class ...Ts> struct BinderDecls<U, BoundTypes<Ts...>> : BinderDecls<U, Ts>... { }; template<typename = void> void addMatch() = delete; template <class T> struct Binder : BinderDecls<T, typename T::bound_types> { }
Then you can write
struct TestType { typedef BoundTypes<int, float> bound_types; int i; float j; }; int main(int argc, char** argv) { Binder<TestType> b; addMatch<&TestType::i>(b); addMatch<&TestType::j>(b); }
source share