ProblemsTemplatesStrip every star
IntermediateTemplates

Strip every star

Context

Generic code received a T that may be a pointer, a pointer to a pointer, or deeper — and it needs the element type at the bottom. One remove_pointer is not enough and you do not know the depth in advance. Type recursion solves this in three lines.

Task

Implement RemoveAllPointers<T>::type: int*** becomes int, int* becomes int, int stays int. std::remove_pointer strips one level — yours strips them all.

Constraints

  • A non-pointer type comes back unchanged
  • Any pointer depth collapses to the pointee at the bottom
  • Qualifiers on the bottom type survive: const int** gives const int
  • Pure compile time

Before you code

  • Where is the recursion base here, and where is the step?
  • Why does the T* specialization match const int* as well? (what is T deduced as)
  • How would you stop the recursion one level early to get "the last pointer"?

Tests

  • #1Pointers of any depth collapse
  • #2Bottom-type qualifiers survive

Hints

Hint 1

The primary template is the recursion base: a non-pointer maps to itself. The T* specialization is the step.

Hint 2

Inside the T* specialization, ask RemoveAllPointers<T> again — typename is required before its ::type.

Editorremove-all-pointers.cpp
Results

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