In my last question I received great help in getting a template specialization to work. Now I need a little extension. I want two specializations for these statements:
int main()
{
// First specialization
holder_ext<person> h1;
holder_ext<person, &person::age> h2;
holder_ext<int> h3;
// Second specialization
holder_ext<person, &person::age, &person::name> h4;
}
My class person looks like this:
class person
{
private:
std::string name_;
int age_;
public:
person(const std::string &name)
: name_(name), age_(56)
{}
void age(int a) { age_ = i; }
void name(const std::string &n) { name_ = n; }
};
The special thing is, that the two member functions have different parameter types. So I can't use the same variadic template member function for both. I tried it with two different variadic templates. But that doesn't work. Also default values for the member functions do not work.
Does anybody have a good hint for me?
This is the solution with one member function (thanks to Pubby):
template < class T, void (std::conditional<std::is_class<T>::value, T, struct dummy>::type::* ...FUNC)(int)> class holder;
template < class T, void (T::*FUNC)(int)>
class holder<T, FUNC>
{
public:
explicit holder() : setter(FUNC) { std::cout << "func\n"; }
private:
std::function<void (value_type&, int)> setter;
};
template < class T>
class holder<T>
{
public:
explicit holder() { std::cout << "plain\n"; }
};
Thanks again in advance!
P.S.: And no, I won't come up in two days with "what must do with three, four, five member functions"? ;-)