Anyway to achieve the following

Johan j at j.nl
Tue Aug 17 10:26:16 UTC 2021


On Monday, 16 August 2021 at 19:30:19 UTC, JG wrote:
> On Sunday, 15 August 2021 at 21:53:14 UTC, Carl Sturtivant 
> wrote:
>> On Sunday, 15 August 2021 at 07:10:17 UTC, JG wrote:
>>> [...]
>>
>> What you are asking for are reference variables. C++ has them: 
>> the example here illustrates the behavior you want.
>> https://www.geeksforgeeks.org/references-in-c/
>>
>> [...]
>
> Thanks for the links and code. Looking at the assembly as 
> suggested by others it seems that after optimization this not 
> too bad.

The problem is not optimization. The semantics of the code are 
different when you use the `Ref!int` solution versus simply 
`int`. You'll see this in template instantiations, as in the 
`writeln` example.

`writeln` is a template and `writeln!int` is something different 
from `writeln!(Ref!int)`. When you force the template 
instantiation to `writeln!int` -- e.g. with an explicit cast, 
https://d.godbolt.org/z/6fKbMh731 -- the `Ref!int` output is the 
same as with `int`.

Regardless, I haven't seen a performance measurement, and it is 
most likely completely irrelevant for the performance of your 
program.

-Johan





More information about the Digitalmars-d-learn mailing list