views:

59

answers:

1

I want to pass in the name of a member variable. I thought I could do this by

template <typename T::*>
void SetVal(T::* newval)
{

};

This obviously doesn't work, but hopefully gets across what I'm trying to do. I want to be able to set a certain member variable of the templated class.

+2  A: 

You can always put compilation-defined constant as template arguments. So here that would be:

template <typename T, typename R, R T::* member>
R& SetVal(T& t, const R& value)
{
   t.*member = value;
   return t.*member;
}

struct A
{
  int a;
};

int main()
{
  A a;
  SetVal<A,int,&A::a>(a, 10);
  return 0;
}
PierreBdR