`in` parameters made useful

Araq rumpf_a at web.de
Fri Aug 21 05:20:45 UTC 2020


On Thursday, 20 August 2020 at 22:19:16 UTC, Andrei Alexandrescu 
wrote:
> On 8/20/20 1:31 PM, IGotD- wrote:
>> This is interesting on a general level as well and true for 
>> several programming languages. Let the compiler optimize the 
>> parameter passing unless the programmer explicitly ask for a 
>> certain way (copy object, pointer/reference etc.).
>
> This has been discussed a few times. If mutation is allowed, 
> aliasing is a killer:
>
> void fun(ref S a, const compiler_chooses S b) {
>     ... mutate a, read b ...
> }
>
> S x;
> fun(x, x); // oops
>
> The problem now is that the semantics of fun depends on whether 
> the compiler chose pass by value vs. pass by reference.

True but in practice it doesn't happen very often. The benefits 
far outweigh this minor downside. Plus there are known ways to 
prevent this form of aliasing at compile-time.


More information about the Digitalmars-d mailing list