ProblemsTemplatesA progression, says static_assert
IntermediateTemplates

A progression, says static_assert

Context

A lookup-table generator takes its sample points as template parameters and only works correctly when they are evenly spaced. Instead of documenting that requirement, enforce it: a wrong instantiation should fail a static_assert with a clear message.

Task

Implement isProgression_v<Ns...> — a compile-time check that a pack of integers forms an arithmetic progression. One or two numbers always qualify; the empty pack does not.

Constraints

  • isProgression_v<1, 3, 5> is true; isProgression_v<1, 2, 4> is false
  • A single value and any pair are progressions; the EMPTY pack is false
  • Negative steps count: <11, 9, 7> is a progression
  • Pure compile time

Before you code

  • Why is the common difference fixed by the first two elements?
  • How do you materialize a value pack into something loopable at compile time?
  • Why define the empty pack as false here?

Tests

  • #1Progressions and non-progressions
  • #2Short packs and the empty pack

Hints

Hint 1

Spill the pack into a constexpr array: constexpr int a[] = {Ns...}; then it is an ordinary loop in a constexpr function.

Hint 2

Guard the empty pack with if constexpr BEFORE declaring the array — int a[0] is not legal C++.

Editorarithmetic-progression-check.cpp
Results

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