ProblemsTemplatesCount the pack — sizeof... is banned
IntermediateTemplates

Count the pack — sizeof... is banned

Context

Recursion over a pack is the universal tool of metaprogramming: the same base-plus-step skeleton later filters types, transforms them and folds them. Counting is its simplest exercise — which is why the one-token sizeof... shortcut is off the table here.

Task

Implement CountTypes<Ts...>::value — the number of types in the pack — WITHOUT using sizeof.... The point of the exercise is to walk a pack with recursion: base case plus one-step case.

Constraints

  • CountTypes<>::value == 0, CountTypes<int>::value == 1, and so on
  • sizeof... must not appear anywhere (the grader checks)
  • Pure compile time, usable in static_assert
  • The recursion peels exactly one type per step

Before you code

  • What is the base case and what is the step?
  • How does the same skeleton turn into Contains or AllSame?
  • Why is sizeof...(Ts) still the right tool outside this exercise?

Tests

  • #1Counts of various packs
  • #2Duplicates and references count as entries

Hints

Hint 1

The primary template handles the empty pack: value = 0. A specialization for <T, Ts...> adds 1 and recurses.

Editorcount-types-no-sizeof.cpp
Results

Hit Submit (or ⌘/Ctrl + ↵) — test results will show up here.