Modern C++ Won't Save Us
Walter Bright
newshound2 at digitalmars.com
Wed Apr 28 03:40:55 UTC 2021
On 4/27/2021 5:17 PM, Paul Backus wrote:
> On Tuesday, 27 April 2021 at 23:38:14 UTC, Walter Bright wrote:
>>
>> As always, when confused about this, rewrite the code in terms of simple ref's
>> and pointers.
>
> I think a large part of the confusion here stems from the fact that `scope ref
> T` behaves very differently from `scope T*`, even though `ref T` and `T*` are
> the same "under the hood."
>
> @safe:
> static int* betty(scope ref S s) { return s.ptr; } // Error
> static int* veronica(scope S* s) { return s.ptr; } // Ok
>
> Unfortunately, there is no documentation of this distinction in the language
> spec (or anywhere else, as far as I know), so the only way anyone can learn
> about it is through trial and error.
`scope` is for pointers, `ref` is for references. That's the distinction.
`ref` never applies to pointers, `scope` never applies to references.
(A class reference is treated as a pointer.)
More information about the Digitalmars-d
mailing list