Struct template instancing inside of class failed

Denis Feklushkin feklushkin.denis at gmail.com
Fri May 2 21:58:50 UTC 2025


Hi!

I'm not sure if this is an issue or if it was intended this way.

```d
class C(alias a) {
     pragma(msg, "class body: ", a.stringof);
     int internal;
     this(int unused){
         pragma(msg, "class ctor: ", a.stringof);
         internal = a;
     }
}

struct S(alias a) {
     pragma(msg, "struct body:", a.stringof);
     int internal;
     this(int unused){
         pragma(msg, "struct ctor:", a.stringof);
         internal = a;
     }
}

class Test {
     int field = 123;

     C!field c;
     S!field s;

     this(){
         c = new C!field(777); // ok
         assert(c.internal == 123);

         // Error: accessing non-static variable `field` requires 
an instance of `Test`
         s = S!field(777);
     }
}

void main() {
     auto t = new Test;
}

```

Compiler output:
```
class body: field
struct body:field
class ctor: this.this.field
struct ctor:field
app.d(20,20): Error: accessing non-static variable `field` 
requires an instance of `Test`
         internal = a;
                    ^
Error dmd failed with exit code 1.
```

I suspect that class stores template parameter "a" in some hidden 
fields and this behaviour impossible for POD structs?



More information about the Digitalmars-d mailing list