std::is_array
std::is_array(C++11) 用于判断一个类型是否为数组,如果为数组,则返回1,否则返回0。
可能实现:
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{
};
该实现是基于模板特化,对于数组类型,继承true_type,否则继承false_type,该工具仅支持普通数组。
实例:
#include <iostream>
using namespace std;
class A{
};
int main()
{
cout << std::is_array<int[]>::value << endl; // true
cout << std::is_array<int[3]>::value << endl; // true
cout << std::is_array<const int[3]>::value << endl; // true
cout << std::is_array<int[][3]>::value << endl; // true
cout << std::is_array<int[3][3]>::value << endl; // true
cout << std::is_array<A[]>::value << endl; // true
cout << std::is_array<int>::value << endl; // false
cout << std::is_array<int*>::value << endl; // false
cout << std::is_array<std