ProblemsTemplatesIs it an array?
IntermediateTemplates

Is it an array?

Context

Arrays are the odd type in C++: int[5] and int[] are distinct types, both decay to int* at the slightest touch, and generic code regularly needs to catch them BEFORE the decay. The standard has std::is_array; here you build it yourself and see why there are exactly two specializations.

Task

Implement IsArray<T>::value: true for int[5] and int[], false for everything else (including int* — a pointer is not an array). Two partial specializations carry the whole task.

Constraints

  • True for T[N] (bounded) and T[] (unbounded)
  • False for non-arrays, including pointers
  • IsArray<> (an empty argument list) is valid and false
  • No std::is_array under the hood

Before you code

  • Why are int[5] and int[] different types, and which specialization catches each?
  • Why is int* false here even though arrays decay to it?
  • How does the variadic primary template make IsArray<> legal?

Tests

  • #1Arrays say yes
  • #2Non-arrays say no

Hints

Hint 1

T[] and T[N] are matched by two different partial specializations — the bound is part of the type.

Editoris-array-trait.cpp
Results

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

Is it an array? — CppForge