ProblemsTemplatesFlip every pair in the tuple
AdvancedTemplates

Flip every pair in the tuple

Context

A table is described by types: each column is a pair of key type and value type. To build the reverse index you need the same table with every pair flipped — and you need it as a TYPE, at compile time. One pack expansion does the whole job.

Task

Implement FlipPairs<Tuple>::type: a tuple of pairs becomes a tuple of the same pairs with first and second swapped. tuple<pair<float,int>> turns into tuple<pair<int,float>>. Types only, no values.

Constraints

  • Works for any number of pairs, including an empty tuple
  • Each pair keeps its position; only first/second swap inside
  • Pure compile time — the tests are static_assert only
  • Do not enumerate arities: one variadic specialization

Before you code

  • How does pattern matching on tuple<pair<A,B>...> bind TWO packs at once?
  • Why does the empty tuple come out of the same specialization for free?
  • Where would you reuse this shape? (transforming any tuple of type-pairs)

Tests

  • #1Pairs flip in place
  • #2Empty tuple flips to itself

Hints

Hint 1

Specialize for std::tuple<std::pair<As, Bs>...> — the compiler deduces the As pack and the Bs pack together, position by position.

Hint 2

The result is the same expansion with the packs swapped: std::tuple<std::pair<Bs, As>...>.

Editorflip-pairs-in-tuple.cpp
Results

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