Detect whether a type is an zero-element array?

arrays, c++, c++11, templates, type-traits

Solution

You can use `std::tuple_size`, as it will also work for `std::array`! See here. Simply use:

std::tuple_size<Type>::value == 0

to check if `Type` is an empty `std::tuple<>` or an empty `std::array<T,0>`.

With the above, the question remains what happens if `Type` is neither a `std::tuple` not a `std::array`. The general approach I see is this:

constexpr bool IsNotTupleOrArray =
  !std::is_class<Type>::value ||
  std::is_same<Type,ExcludeThisClass>::value ||
  sizeof(Type)>1 || // non-portable, works for GCC 4.8+
  ...;

std::conditional< IsNotTupleOrArray,
                  std::false_type,
                  std::tuple_size<Type> >::type::value;

which basically means that you have to explicitly exclude other types. For example `is_class<Type>` excludes all fundamental types like `int`, pointers, etc.

Problem

Consider the following function : ``` template <typename Type> void f(const Type& x) ``` I would like to do something special (without specialization) in it whether the passed type is an empty `std::tuple` or an empty `std::array`. For a tuple of nu elements, I can use `std::is_same<Type, std::tuple<>>::value` but what trick can I use to detect an zero-element array ? (I am searching for a solution that do not require the creation of another function or class, ...)

Original source