0

I'm in need of some compile time check if a template type passed into a templated function is any instantiation of std::array

Like

IsStdArray<std::array<float, 12>>::value; // should evaluate to true
IsStdArray<std::array<int, 1000>>::value; // should evaluate to true
IsStdArray<std::vector<float>>::value;    // should evaluate to false
IsStdArray<std::string>::value            // should evaluate to false

I'm especially struggling to come up with anything that is independent of the array size. Note, that a function returning a constexpr bool would be fine as solution too!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
PluginPenguin
  • 1,576
  • 11
  • 25
  • This sounds like it might be a coding solution to a design problem. What is the actual problem that you're trying to solve? – Pete Becker Apr 16 '20 at 15:09

1 Answers1

4

You can partially specialise a trait class.

template<typename T>
struct IsStdArray : std::false_type {};

template<typename T, std::size_t N>
struct IsStdArray<std::array<T, N>> : std::true_type {};
Caleth
  • 52,200
  • 2
  • 44
  • 75