While you want to get a data element from a set of data elements of the same type, you can use a pointer to the data element:
template <int Foo::*M> void cout_member(Foo foo) { std::cout << (foo.*M) << std::endl; }
And use it like:
cout_member<&Foo::a>(foo);
If you want to specify the type as well, you can do this:
template <typename T, T Foo::*M> void cout_member(Foo foo) { std::cout << (foo.*M) << std::endl; }
And use it like:
cout_member<int, &Foo::a>(foo);
Just out of curiosity, the second snippet will be even simpler in C ++ 17:
template <auto M> void cout_member(Foo foo) { std::cout << (foo.*M) << std::endl; }
Take a look at wandbox ;
source share