variable _param_0 cannot be read at compile time

Simen Kjærås simen.kjaras at gmail.com
Wed Aug 8 13:13:42 UTC 2018


On Wednesday, 8 August 2018 at 12:57:43 UTC, learnfirst1 wrote:
> Why this is a error ?
>
> ```
> struct S {
>         bool v;
>         string x;
> }
>
> S* add(A...)(ref A a) {
>         __gshared s = S(a);
>         return &s;
> }
>
> void main(){
>         auto p = add(true);
> }
> ```
>
> test.d(9): Error: variable _param_0 cannot be read at compile 
> time

__gshared and static need to be initialized with a value known at 
compile-time. You're trying to give it a run-time value. You can 
set this after it's first created:

S* add(A...)(ref A a) {
     __gshared S s;
     s = S(a);
     return &s;
}

That's a little kludgy, but apparently that's how it be.

You also get another error message:
> Error: function test.add!bool.add(ref bool _param_0) is not 
> callable using argument types (bool)

That's because add takes its arguments by ref, and true is a 
literal that has no canonical address, and thus can't be passed 
by ref. You might consider using auto ref.

--
   Simen


More information about the Digitalmars-d-learn mailing list