Assume the following toy class, and modern compilers (recent gcc for example).
template <typename T>
class SomeVec {
public:
...
virtual T get(const int index) = 0;
}
The application involves a fair amount of number crunching based on values stored in SomeVec subclasses, T being a primitive type, say int
. However, the practice of stl containers
and boost::numeric::ublas::vector
is to return stored values via (const) reference
.
I wondered the performance differences this involve. In this question it is shown that array element access by value and vector element access by reference results in the same code, so I assume the compiler can in some cases optimize stuff away.
Now my questions are:
(1)
stl
andublas
are templated, while my solution requires virtual methods. Does this hinder modern compilers' ability to optimize code?(2) If the compiler could not optimize to return the const atomic reference as value, then do I assume it right that the virtual method call and the dereferencing cost approximately the same? Or is one significantly more expensive than the other?
Thanks!