Modern C++ Won't Save Us

Walter Bright newshound2 at digitalmars.com
Tue Apr 27 23:38:14 UTC 2021


On 4/27/2021 4:33 AM, MoonlightSentinel wrote:
> On Tuesday, 27 April 2021 at 10:53:10 UTC, Vladimir Panteleev wrote:
>> What does `scope` mean on a function declaration?
> 
> It applies to the `this` pointer for member methods, just like `return`.
> (Allthough `scope` is implicitly applied for structs IIRC)

This isn't quite right. For structs,

     struct S {
         int* ptr;
         int* hello() scope;
	static int* betty(scope ref S s);
     }

The `this` reference is passed by `ref`. So, `hello` is exactly equivalent to 
`betty`.

The `ref` already prevents returning the address of a struct. The `scope` 
prevents the return of `s.ptr` and equivalently `this.ptr`.

This is likely why, in the bug report, the `ptr` field was necessary.

As always, when confused about this, rewrite the code in terms of simple ref's 
and pointers.


More information about the Digitalmars-d mailing list