Struct template instancing inside of class failed

H. S. Teoh hsteoh at qfbox.info
Fri May 2 23:15:24 UTC 2025


On Fri, May 02, 2025 at 09:58:50PM +0000, Denis Feklushkin via Digitalmars-d wrote:
> 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);
[...]

Try `S!(this.field)(777)` maybe?

In any case, passing a class-local variable via an alias into a template
parameter that tries to access it from inside a function seems rather
dangerous and needlessly complex.  Why not just pass the value directly,
or a reference/pointer to the variable?


T

-- 
We are in class, we are supposed to be learning, we have a teacher... Is it too much that I expect him to teach me??? -- RL


More information about the Digitalmars-d mailing list