Reading through previous answers, I feel like I may have a design problem, but even if the answer is academic I'd still like to know if it is possible. I've been programming in Python for awhile and it shows. I'm trying to create something like a setattr
access on an object. By hand it looks like:
template<class T, class U>
void set_parameter_sigma(T &S, U val) {
for(auto &x: S) { x.sigma = val; }
}
template<class T, class U>
void set_parameter_epsilon(T &S, U val) {
for(auto &x: S) { x.epsilon = val; }
}
template<class T, class U>
void set_parameter_length(T &S, U val) {
for(auto &x: S) { x.length = val; }
}
What I'd like is something that looks like the following pseudocode:
template<class T, class U, class N>
void set_parameter(T &S, U val) {
for(auto &x: S) { x.N = val; }
}
I could call it like set_parameter(foo, 2.0, "epsilon")
and the compiler would create the set_parameter_epsilon
function automagically. While I'm sure the boost can do this, I'd prefer to see a STL-only version if possible.