Performance improvements for D / DMD compiler.
Bill Baxter
dnewsgroup at billbaxter.com
Sat Jan 20 00:31:51 PST 2007
Walter Bright wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>> No, and for a good reason:
>>
>> foo(LargeStruct s1, LargeStruct s2)
>> {
>> ...
>> }
>> ...
>> LargeStruct s;
>> foo(s, s);
>>
>> Nobody would enjoy hidden aliasing of s1 and s2.
>
> There's another aliasing case:
>
> LargeStruct s;
> foo(s);
> ... here some other thread modifies s ...
>
> And foo() suddenly has its argument values change unexpectedly.
>
> Value and reference type usages mostly overlap, but they are
> fundamentally different, and having the compiler switch between the two
> in some implementation-defined manner (as suggested by others more than
> once) is not going to work.
>
> If one is needing reference behavior from a struct, seriously consider
> making it a class instead. Or even just take the address of it and pass
> the pointer.
If we're going to pass a pointer to it, then we might as well just make
it inout and avoid having to explicitly use '&' to take the address.
The question is how can we write functions in D that take big structs as
parameters in such a way that it is:
A) efficient and
B) guaranteed not to change the caller's value
In C++ the answer is const LargeStruct&. In D the answer is...
--bb
More information about the Digitalmars-d
mailing list