Using in as a parameter qualifier

Jonathan M Davis jmdavisProg at gmx.com
Fri May 31 12:43:04 PDT 2013


On Friday, May 31, 2013 23:19:57 Shriramana Sharma wrote:
> > However, :) if you are going to make a copy of the argument anyway, always
> > take a struct by-value. That works with both lvalue and rvalue arguments
> > and in the case of rvalues, you will get the automatic move semantics.
> > 
> > My choice is by-value for structs unless there is any reason not to.
> 
> So where is the cut-off point? I mean, by-value means a copy is done
> every time the function is called right?

The compiler will move an object rather than copy it when it can. So, for 
instance, if you pass the function a temporary, it'll move that temporary 
rather than copying it. However, if it's called with an lvalue, odds are that 
it's going to have to make a copy (though it might be moved if it were the 
last time in the caller that the variable was referenced).

> So how heavy (in terms of
> sizeof) would a struct have to be to make passing by ref more
> efficient than passing by value?

That would depend on the compiler. You'd have to do tests to see. Certainly, 
for anything that's small enough to fit in a register, it's likely to be faster 
to pass by value. But how much beyond that the struct has to grow before it's 
large enough that passing by reference would be cheaper, I don't know. A lot 
that the compiler does could affect that. The only way to know for sure is to 
test it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list