Struggling to implement parallel foreach...

Timon Gehr timon.gehr at gmx.ch
Fri Jun 14 01:55:25 UTC 2019


On 14.06.19 03:40, Manu wrote:
> void test()
> {
>          int x;
>          void fun() shared
>          {
>              x++; // <-- error here
>          }
>          fun();
> }
> 
> And I got this error:
> 
> Error: `shared` function `test.fun` cannot access non-shared data `x`
> 
> That looks like a bug.

It's by design. It's not pretty that the qualifiers have a different 
meaning for member functions and nested functions (because the nested 
function meaning could be useful for member functions too), but it's 
what we have.

> `fun` is a delegate, and it's shared, which
> means its `this` pointer is shared, which means `this.x` should be
> shared... but that seems not to be the case.
> ...

There is no `this` pointer. For local functions, `shared` means that 
every variable accessed in the context is shared.

> I think this is a bug. The compile error should have been that I was
> unable to call fun() with a non-shared delegate (since the calling
> scope is not shared).

There is no way to make the "calling scope shared".


More information about the Digitalmars-d mailing list