ProblemsTemplatesAll the same, says the compiler
IntermediateTemplates

All the same, says the compiler

Context

A variadic constructor wants to accept only a uniform list — say, N coordinates of the same type. To reject a mixed call at compile time you first need a way to ASK the question at compile time: are all these types identical? That is a metafunction: types in, constant out.

Task

Implement allSame_v<Ts...> — a compile-time check that every type in the pack is the same. allSame_v<int, int> == true, allSame_v<int, long> == false. The tests run entirely in static_assert.

Constraints

  • allSame_v<> and allSame_v<T> are true (zero or one type counts as uniform)
  • Differences in const and reference count: allSame_v<int, const int> is false
  • Pure compile time — usable inside static_assert
  • No specialization spam: handle any pack length generically (recursion or a fold)

Before you code

  • Why is std::is_same_v the right primitive here, and what exactly does it compare?
  • How does a fold expression collapse a pack of bools?
  • Where would you use allSame_v in a real API? (variadic constructors, tuples)

Tests

  • #1Uniform and mixed packs
  • #2Zero and one type are uniform
  • #3const and references matter

Hints

Hint 1

It is enough to compare every type with the FIRST one — they are all equal exactly when each equals the first.

Hint 2

A fold over && does the rest: (std::is_same_v<First, Rest> && ...). Mind the empty pack — an empty && fold is true.

Editorall-same-metafunction.cpp
Results

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