Consider passing an argument via a constant vs via a reference to a constant
T function(T const x) {}
T function(T const& x) {}
Constant already signify that the value will not be changed. Then why would a programmer need to specify whether to pass the argument by value or reference?
Should this optimization not be a decision made by the compiler?
For example, if the argument is of primitive type, such as char
or int
, then it is more cost effective to spawn a copy for the function to use.
If the argument of complex type, such as std::string
or std::vector
or even a user defined one, it it more cost effective for the function to handle it through reference.
However, these decision are not dilemma where the programmer have to make a compromise that would impact performance in any way.
In my point of view, the use of reference to constant is redundant. Stating that an argument is of a constant nature should be enough, and the compiler should be the one that handle whether to pass it by value or by reference.