www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - variable _param_0 cannot be read at compile time

reply learnfirst1 <learnfirst1 gmail.com> writes:
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
test.d(14): Error: template instance `test.add!bool` error 
instantiating
test.d(14): Error: function test.add!bool.add(ref bool _param_0) 
is not callable using argument types (bool)
test.d(14):        cannot pass rvalue argument true of type bool 
to parameter ref bool _param_0



I try pass some into the template to return a __gshared var 
pointer.  the A length is dynamic.
Aug 08 2018
next sibling parent learnfirst1 <learnfirst1 gmail.com> writes:
On Wednesday, 8 August 2018 at 12:57:43 UTC, learnfirst1 wrote:
 Why this is a error ?
this code example can explain what I am try to do here: struct M { int i; S*[100] s; } struct S { M* mp; bool x; } S* add(A...)(ref A a) { __gshared s = S(a); alias m = a[0]; m.s[m.i++] = &s; return &s; } void main(){ __gshared M m = M(0); __gshared S s = S(&m, false); m.s[m.i++] = &s; auto p = add(&m, true); // variable _param_0 cannot be read at compile time } because S has the optional ctor parameters, I can not use template alias param here( some how not work) .
Aug 08 2018
prev sibling parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
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
Aug 08 2018
parent reply learnfirst1 <learnfirst1 gmail.com> writes:
On Wednesday, 8 August 2018 at 13:13:42 UTC, Simen Kjærås wrote:
 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
is there a way to pass A a... as alias into template ? so the code can be just like this: __gshared S s = S(&m, false); the binary size is short since in this case the s is static build int. and run a bit fast.
Aug 08 2018
parent reply Kagamin <spam here.lot> writes:
struct M {
         int i;
         S*[100] s;
}
struct S {
         M* mp;
         bool x;
}

S* add(A...)() {
         alias m = A[0];
         __gshared s = S(&m,A[1..$]);
         m.s[m.i++] = &s;
         return &s;
}

void main(){
         __gshared M m = M(0);
         __gshared S s = S(&m, false);
         m.s[m.i++] = &s;
         auto p = add!(m, true);
}
Aug 09 2018
parent learnfirst1 <learnfirst1 gmail.com> writes:
On Thursday, 9 August 2018 at 13:42:03 UTC, Kagamin wrote:
 struct M {
         int i;
         S*[100] s;
 }
 struct S {
         M* mp;
         bool x;
 }

 S* add(A...)() {
         alias m = A[0];
         __gshared s = S(&m,A[1..$]);
         m.s[m.i++] = &s;
         return &s;
 }

 void main(){
         __gshared M m = M(0);
         __gshared S s = S(&m, false);
         m.s[m.i++] = &s;
         auto p = add!(m, true);
 }
this is what I want! thanks.
Aug 09 2018