Passing structs to functions

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 2 12:25:37 PDT 2016


On Saturday, 2 July 2016 at 18:47:31 UTC, phant0m wrote:
> On Saturday, 2 July 2016 at 18:43:51 UTC, ketmar wrote:
>> void boo() (in auto ref MyStruct s) { ... }
>>
>> this will accept both lvalues and rvalues, and will avoid 
>> copying if it can.
>
> Thank you! Could you please explain what does "auto" in this 
> context mean?

basically, what you think it does. ;-)

it means "use ref if you can, but fallback to copy if you can't". 
so it accepts both:

   MyStruct s;
   boo(s);

and

   boo(MyStruct());

note the first "()", though: this is effectively a template 
function, which compiler will instantiate either with "ref" or 
without it.


More information about the Digitalmars-d-learn mailing list