About ref used for performance reasons with struct

kinke noone at hotmail.com
Mon Feb 11 07:26:09 PST 2013


On Monday, 11 February 2013 at 15:12:07 UTC, deadalnix wrote:
> On Monday, 11 February 2013 at 14:54:48 UTC, kinke wrote:
>> I'd propose a small change so that suited structs are passed 
>> transparently byref only if the parameter is not mutable, 
>> e.g., for a function foo(const BigStruct s). The compiler 
>> would therefore not need to analyze the code flow in the 
>> callee to determine if the parameter is modified and hence a 
>> copy is needed.
>
> That is overly restrictive. See sample code given in this 
> thread, foo couldn't get the struct by ref in such a case if A 
> contains any indirection.

Not sure what you mean by A containing any indirections, but I'd 
simply rewrite your example as follows:

void foo(const A a) { // byref passing desirable
     bar(a); // bar's parameter is mutable => pass a copy (byval)
}

void bar(A a) { // byval passing
     a.member = 5;
}


More information about the Digitalmars-d mailing list