I have template class which looks like this:
template<int n=3> struct Vec{
double Values[n];
};
Of course I can access elements of this class directly or by method, for example:
double& x(){
return Values[0];
}
But if I would like to make more intuitive code: instead of
Vec<3> v;
v.x() = 5.2;
I would like to let user do it by:
v.x = 5.2
. It's not only a whim. If I have existing library which uses simple vector structures of a form like struct{double x,y,z;}
, I could by method of duck-typing create my template class to be efficient in this case. Of course I could (probably, I'm not sure) pass to this library predefined structure of referenes - like:
struct{double &x=v.Values[0], &y=v.Values[1], &z=v.Values[2];}
but I am afraid that's not the simplest way to get a goal.
Ah - of course I could add reference-type parameters in my structure, but this would cause in increase of a size of elements.
Here's one way to get the same syntactic effect:
Going the other way is not quite so pleasant. There is no such thing as an array of references, so one workaround is to resort to an array of
std::reference_wrapper
: