[std-interval] passing by value vs reference

Fernando Cacciola fernando_cacciola at datafull.com
Wed Apr 5 12:39:51 PDT 2006


Sylvain Pion wrote:

>
> So, as for the proper text in the proposal, let me propose:
> - to have the text of the function prototypes use pass-by-value,
>   as it fulfills the "advocate safer programming style" argument
>   of Lawrence, and it is also shorter.
> - a note at the beginning mentionning that the implementation
>   is free to use pass-by-reference.
>
FWIW, I agree with this for the reasons Sylvain gave (comparing the 
other choices).
The note should say pass-by-const-reference though.

Regarding the pass by-value vs by-const-reference discussion, it is 
important to consider that the fields of this POD are floating point types.
A compiler will use a register to pass the address of the interval in 
the pass-by-const-reference case, but that's the wrong register...
When the interval fields are actually used inside the called function, 
they will have to be loaded in the FPU registers.

The most efficient scenario occurs when the compiler elides the 
copy-constructor and the calling function had already the fields of the 
interval arguments (typically 2) already loaded in FPU registers. In 
that case, there is no argument passing at all.

In my experience, this sort of optimization is common when the argument 
is passed-by-value (yes, it is common); yet I've never seen it in a 
pass-by-const-reference case.

Fernando Cacciola
SciSoft
http://fcacciola.50webs.com






More information about the Std-interval mailing list