Simulating reference variables using `alias this`

Carl Sturtivant via Digitalmars-d digitalmars-d at puremagic.com
Wed May 10 12:31:42 PDT 2017


On Wednesday, 10 May 2017 at 17:48:53 UTC, MrSmith wrote:
> On Wednesday, 10 May 2017 at 17:12:11 UTC, Carl Sturtivant 
> wrote:
>> Here's the beginning of an interesting little experiment to 
>> simulate reference variables using `alias this` to disguise a 
>> pointer as a reference. Could add a destructor to set the 
>> pointer to null when a reference goes out of scope.
>> ...
>
> I've used this code for similar purpose:
>
> alias TextEditorSettingsRef = TextEditorSettings*;
> alias TextEditorSettingsConstRef = const(TextEditorSettings)*;
> struct TextEditorSettings
> {}
>
> But you need to have two aliases (templates) for const and 
> non-const refs, since using:
> const TextEditorSettingsRef editor;
> does:
> const(TextEditorSettings*)
> not
> const(TextEditorSettings)*
>
> What do you think?

Works if you use new to get struct pointers. And . knows it's a 
pointer so you get the right effect. But I want to see if D can 
simulate general reference variables safely.



More information about the Digitalmars-d mailing list