As mentioned in @black, optimization is compiler and platform dependent. However, we usually expect a series of optimizations to occur day by day using a good optimizing compiler. For example, we expect to include functions, register allocation, conversion of constant multiplications and divisions to bit shifts, when possible, etc.
To answer your question
Can modern x86-64 compilers be smart enough to pass a priority argument by value, rather than a link, if the priority type can fit in a register?
I will just try it. See for yourself:
This is the code:
template<typename T> T square(const T& num) { return num * num; } int sq(int x) { return square(x); }
GCC -O3 , -O2 and -O1 reliably perform this optimization.
Clang 3.5.1, on the other hand, does not seem to perform this optimization.
Should such an optimization be counted on? Not always, but not quite - the C ++ standard does not say anything about when such an optimization can happen. In practice, if you use GCC, you can "expect" optimization.
If you are absolutely sure that such optimization will happen, you will want to use the specialization of the template .
source share