If you pass the int by const reference, you'll end up paying the penalty of one (unnecessary) layer of indirection to access its value unless a very smart optimizer detects that it's OK to just pass the int by value under all circumstances.
Sometimes passing and int by (const) reference makes sense, but really only if you are writing templated code and don't want to create additional specializations for primitive data types like int
. Under normal circumstances you are better off passing the int
by value instead of const reference, especially as on a lot of hardware, the int can be passed into the function in a register when you're dealing with functions with signatures like the ones you have above. And even if not, it's right there on the stack with good locality of reference.
With a double
the picture changes somewhat because on some architectures it's more efficient to pass a reference or pointer to a double into a function rather than the value itself. However on recent hardware, you're most likely to lose performance due to the layer of indirection compared to just passing the value.
In both cases, a compiler/linker with fairly aggressive inlining and especially link time code generation would be able to optimize the code to avoid any parameter passing on the stack if you're dealing with smaller utility functions that the code generator will inline.