This answer on my other question relates to your problem pretty well. With some minor changes, you will get the following:
template<class C, class T> T get_attribute(const C& instance, T (C::*func)() const) { return (instance.*func)(); }
Assuming the following:
struct Foo { int getSomething1() const; std::string getSomething2() const; someClass getSomething3() const; };
You can use it as follows:
Foo foo; int value = get_attribute<Foo, int>(foo, &Foo::getSomething1); std::string value = get_attribute<Foo, std::string>(foo, &Foo::getSomething2); someClass value = get_attribute<Foo, someClass>(foo, &Foo::getSomething3);
You can, of course, convert get_attribute to a functor to bind some or all of the arguments.
source share