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.