Using a function in a virtual class to determine a parameter

I am trying to use a function defined in a virtual class of a package to determine a parameter and I have run into an issue. Is there an elegant way to do this?. Here’s a snippet of code that causes the issue - the compiler complains of that a non-static class member cannot be accessed via the class scope resolution operator but I am not sure how I could resolve this.

package test_pkg;

   virtual class const_func #(parameter ARRAY_SIZE = 8);

      function automatic int num_sel 
        (
         input logic [(ARRAY_SIZE-1):0] sel 
         );
         int                            num;
         num = 0;
         for(int i = 0; i < ARRAY_SIZE; ++i) 
           if (sel[i])
             num = num + 1;
         return(num);
      endfunction: num_sel

   endclass: const_func

endpackage

module test 
  #(
    parameter int                     NUM_WORDS = 32,
    parameter logic [(NUM_WORDS-1):0] SM_WORD_SEL = 'h55555555,
    parameter int                     SEL_NUM_WORDS = test_pkg::const_func#(.ARRAY_SIZE(NUM_WORDS))::num_sel(SM_WORD_SEL)
    )
   ( input                       clk,
     input                       rst_n,
     input [(SEL_NUM_WORDS-1):0] sel, 
     output                      out
     );

endmodule

Declare your function as

static function int num_sel 
1 Like

Thanks this worked. The reason I used automatic is because I thought it would help if the function is used in a recursive manner although in this particular usage its not. I am not sure if this is possible but would a recursive call be made to the class even if the same parameter is passed to the class so declaring the function as static wouldn’t matter?

Methods declared inside classes always have automatic lifetimes. The static keyword in front of the function is a class member modifier indicating it is associated with the class type, not an instance(object) of the class.

See What is the exact difference between static tasks/functions and automatic tasks/functions ? please explain with a clear example - #2 by dave_59