Why does std :: is_array return false for std :: array?

Since std :: array <> and std :: is_array <> were introduced in C ++ 11, it seems very strange that this will not compile:

#include <array>
#include <type_traits>

static_assert(std::is_array<std::array<int,2>>::value);

Is there an easy way to check if there is something in the array, including features T[N]and std::array<T,N>?

+4
source share
3 answers

ISO / IEC 14882: 2011, § 20.9.4.1, table 47 says the following:

  • Template: template struct is_array;

  • Condition: T - array type (3.9.2) of known or unknown degree

  • Comment: an array of class templates (23.3.2) is not an array type.

therefore, the statement should fail.

is_array, @0x499602D2, , , .

+4

Cppreference :

template<class T>
struct is_array : std::false_type {};

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 {};

, std::array . :

template<class T>
struct is_array : std::is_array<T> {};
template<class T, std::size_t N>
struct is_array<std::array<T, N>> : std::true_type {};
+1

std::is_array true , T[] T[N]. std::array .

std::is_array true_type std::array ; , . std, , . ( , std ).

is_array:

namespace notstd {
  template<class T>
  struct is_array:std::is_array<T>{};
  template<class T, std::size_t N>
  struct is_array<std::array<T,N>>:std::true_type{};
  // optional:
  template<class T>
  struct is_array<T const>:is_array<T>{};
  template<class T>
  struct is_array<T volatile>:is_array<T>{};
  template<class T>
  struct is_array<T volatile const>:is_array<T>{};
}

notstd::is_array<T> C ++ std::array.

+1

Source: https://habr.com/ru/post/1662595/


All Articles