Nested parameter extraction

I have code with nested template parameters:

class datatype#(parameter int W);
    bit signed [W-1:0] s;
    static const int c_W=W;
endclass

class data_packer#(type T, int W);
    static function void pack(T in);
        $display("data_packer: W equals %0d",W);
    endfunction
endclass

class C#(type T, int Z);
    T x;
    function void f();
        //data_packer#(T,T::c_W)::pack(x);
        data_packer#(T,Z)::pack(x);
    endfunction
endclass

...

    C#(datatype#(20),20) myC=new;
    myC.f();


This works but I need to pass the ‘20’ twice. So I wanted to do:

class datatype#(parameter int W);
    bit signed [W-1:0] s;
    static const int c_W=W;
endclass

class data_packer#(type T, int W);
    static function void pack(T in);
        $display("data_packer: W equals %0d",W);
    endfunction
endclass

class C#(type T);
    T x;
    function void f();
        data_packer#(T,T::c_W)::pack(x);
        //data_packer#(T,Z)::pack(x);
    endfunction
endclass

...

    C#(datatype#(20)) myC=new;
    myC.f();


I.e. replacing the Z parameter by the ‘extracted’ T::c_W.

This compiles fine, but doesn’t run. Is this not possible in SV or am I making a big mistake somewhere?

In reply to NiLu:

I guess scope resolutions are not allowed in specialization of parameterized class.

In reply to cool_cake20:

The thing is that Questa just gives a segmentation fault. If it were something not lrm compliant I would have expected an error of some kind. It makes me think I am just doing something wrong.

The problem here is that you cannot override a parameter (class or module) with a variable. Parameters can only be assigned with expressions made up of constants or other parameters. A const variable is not a constant; it’s just a variable that gets initialized once and never written again. You can even have non-static const variables that get initialized each time the class get constructed and never written to again during the life of the object.

Why do you even need c_W? You can just use T::W

Don’t know why you got such a poor error message. Someone will be looking into it.

In reply to dave_59:

I was trying the c++ way. I didn’t know you can access parameters via :: directly.
Thanks!