VLSI DV Interview Puzzles · All levels

Same Specialization via typedef Alias

Do these two handles share the same static counter or maintain two counters?

Puzzle

Difficulty: Medium · Puzzle 2 of 6 · Topic: Parameterized Class Puzzles

Do these two handles share the same static counter or maintain two counters?

Code

systemverilog
class pkt #(int W = 8);
  static int created = 0;
  function new();
    created++;
  endfunction
endclass

typedef pkt#(32) pkt32_t;

initial begin
  pkt#(32) a = new();
  pkt32_t  b = new();
  $display("pkt#(32)::created=%0d", pkt#(32)::created);
  $display("pkt32_t::created=%0d", pkt32_t::created);
end

Hint

A typedef alias does not create a new specialization.

Step-by-step solution

diagram
1) `pkt32_t` is only an alias for exactly `pkt#(32)`.
2) Both objects increment the same specialization static `created`.
3) Both displays print 2.

Answer

Answer: They share one counter; both lines print 2 because typedef alias points to same specialization.

Why candidates get it wrong

Mistaking typedef for a new derived type causes wrong assumptions about static sharing.

Interviewer follow-up

Would `pkt#(16)::created` change in this snippet?

Related topics