@safe function with __gshared as default parameter value

data pulverizer data.pulverizer at gmail.com
Wed Apr 8 18:50:16 UTC 2020


On Wednesday, 8 April 2020 at 16:53:05 UTC, Anonymouse wrote:
> ```
> import std.stdio;
>
> @safe:
>
> __gshared int gshared = 42;
>
> void foo(int i = gshared)
> {
>     writeln(i);
> }
>
> void main()
> {
>     foo();
> }
> ```
>
> This currently works; `foo` is `@safe` and prints the value of 
> `gshared`. Changing the call in main to `foo(gshared)` errors.
>
> Should it work, and can I expect it to keep working?

According to the manual it shouldn't work at all 
https://dlang.org/spec/function.html#function-safety where it 
says Safe Functions: "Cannot access __gshared variables.", I 
don't know why calling as `foo()` works.



More information about the Digitalmars-d-learn mailing list