I am making a library that involves 3-D coordinates, and discovered that there are two names for the components of a 3-D angle: yaw-pitch-roll and heading-elevation-bank
So I made the following (done in c++11):
struct Angle {
float heading;
float elevation;
float bank;
float &yaw = heading;
float &pitch = elevation;
float &roll = bank;
// Some Constructors (copy and assignment have to be made manually)
}
Which has the benefit of keeping the notation equivalent between the two name-schemes. For example:
Angle angle;
rotate(angle.yaw); // this is equivalent to rotate(angle.heading)
I was wondering if the compiler would figure out the references were unnecessary, or if it would keep the pointers in the structure.
Also, is there a better way to have two names for one member?
In 99.9% of the cases, the pointers will be kept in the structure. I don’t see a way for the compiler to exclude them over translation units. Esp since your syntax is invalid, and you’d have to initialize the references in the constructor, which could very well be hidden. So there would be no way for it to know which reference references which member.
There could be some performance overhead as well. For example:
Indeed, internally, references act like pointers. So the extra
movmakes up for dereferencing that pointer.I wouldn’t worry about it though, would instead care about the style. And having two member accounting for the same thing…just seems wrong.