检测阵列类型不起作用

The*_* do 0 c++ templates

template<typename T> 
class CompoundT {           // primary template 
  public: 
    enum { IsPtrT = 0, IsRefT = 0, IsArrayT = 0, 
           IsFuncT = 0, IsPtrMemT = 0 }; 
    typedef T BaseT; 
    typedef T BottomT; 
    typedef CompoundT<void> ClassT; 
};

template<typename T, size_t N> 
class CompoundT <T[N]> {    // partial specialization for arrays 
  public: 
    enum { IsPtrT = 0, IsRefT = 0, IsArrayT = 1, 
           IsFuncT = 0, IsPtrMemT = 0 }; 
    typedef T BaseT; 
    typedef typename CompoundT<T>::BottomT BottomT; 
    typedef CompoundT<void> ClassT; 
}; 
Run Code Online (Sandbox Code Playgroud)

在主要:

template<class T>
bool isArray(T a)
{
    return CompoundT<T>::IsArrayT;
}

int _tmain(int argc, _TCHAR* argv[])
    {
        int a[10];
        cout << isArray(a);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用?这个例子来自"模板完整指南"ch.19.2.

Arm*_*yan 5

因为isArray必须引用,否则如果你按值获取数组,它就像你拿一个指针一样:)

template <class T>
bool isArray(const T& ) {...}
Run Code Online (Sandbox Code Playgroud)

因为

void f(int a[10]);
Run Code Online (Sandbox Code Playgroud)

void f(int* a);
Run Code Online (Sandbox Code Playgroud)

是等同的声明.