Template function on struct members

Viewed 1359

Is there a way to code a single template function able to run on different members of a given struct ?

A wrong example would look like :

struct Foo
{
  int a, b;
}

template <MEMBER x> //which does not exist 
cout_member(Foo foo)
{
  cout << foo.x << endl;
}

int main()
{
  Foo foo; 
  cout_member<a>(foo);
  cout_member<b>(foo);
  return 0;
}

I imagined an answer based on a switch, but I then wondered if this switch would be tested on run-time (what I would like to avoid) or on compile-time ?

2 Answers
Related