Struct template instancing inside of class failed
Denis Feklushkin
feklushkin.denis at gmail.com
Sat May 3 09:38:47 UTC 2025
On Friday, 2 May 2025 at 22:51:24 UTC, monkyyy wrote:
>> I suspect that class stores template parameter "a" in some
>> hidden fields and this behaviour impossible for POD structs?
>
> Theres allot of spooky things here
The main question for me is: why it worked for the class, but not
for the struct?
>
> it will compile if you add static to `static int field = 123;`
>
> if you want a spooky reference to a variable
(Or function, or delegate)
> I suggest my "innate" code avoid this construction logic your
> playing with
The whole idea looks something like this:
```d
struct S(alias outer_handler) {
int internal_field = 1;
this(int ctor_arg){
internal_field = ctor_arg;
assert(outer_handler + internal_field == 124, "ctor
failed");
}
void someMethod() {
assert(outer_handler + internal_field == 124, "method
failed");
}
~this(){
// imitate some destruction process
assert(outer_handler + internal_field == 124,
"destruction failed");
}
}
class Test {
int field = 123;
ref getField() => field;
alias S_Inst = S!getField;
S_Inst s;
this(){
// Error: calling non-static function `getField` requires
an instance of type `Test`
s = S_Inst(1);
}
}
void main() {
auto t = new Test;
}
```
More information about the Digitalmars-d
mailing list