Question

Looking at the example implementation of std::is_array, they have the following code:

template<class T>
struct is_array<T[]> : std::true_type {};

template<class T, std::size_t N>
struct is_array<T[N]> : std::true_type {};

When will the T[] specialization be matched and not the T[N] specialization? Why the need for two? I assume this T[] isn't the same as it would be in a function argument where it means the same thing as a pointer right?

Was it helpful?

Solution

The type T[] is an incomplete type, it is known to be an array but the size (which is also part of the type) is unknown at this point. It can be used in some contexts and in those contexts you may want to check whether the variable that is declared as such is an array or not. While the type of the variable is still incomplete the second specialization of std::array would not match, as the size is unknown.

// test.h
struct Test {
   static int data[];
};
// test.cpp
int Test::data[10];

An TU that only includes the header but has no visibility of test.cpp may want to test whether Test::data is an array or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top