Say I have a simple vector class...
In the constructors, the data is simply copied from the input to the private data member. I use this class quite extensively in some code that needs to run fast so performance considerations are important. A common example is something like this...
Vector3 operator+(const Vector3& vec) const;
Vector3 operator-(const Vector3& vec) const;
float dot(const Vector3& vec) const;
I was wondering if modern compilers would be able to recognize that the operations performed on v1, v2, and v3 are constant and therefore can be performed directly on the variable data without creating new objects. Or, assuming this really is a bottleneck, should I avoid encapsulation here in the name of speed?
//initialize and fill data
Vector3 v1(&data), v2(&data), v3(&data);
float result = v1.dot(v2 - v3);