开发者

Detecting array type do not works

template<typename T> 
class Com开发者_开发技巧poundT {           // 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; 
}; 

and in main:

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

Why this doesn't work? This example is from "Templates the complete guide" ch.19.2.


Because isArray must take reference, otherwise if you take an array by value it's the same as if you take a pointer :)

template <class T>
bool isArray(const T& ) {...}

because

void f(int a[10]);

and

void f(int* a);

are equivalent declarations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜