tags:

views:

49

answers:

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

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.

+5  A: 

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.

Armen Tsirunyan
@Armen wow man, you're still here, I mean whole day? ;) Thanks btw.
There is nothing we can do
@There: No, as a matter of fact, I just got home 5 min ago. Was my answer helpful?
Armen Tsirunyan
@Armen yes, thank you. Can't accept though. Have to wait 15 minutes.
There is nothing we can do
@There The same is with functions, if you take a function by value, it's the same as if you took a pointer to function. The same is with templates. If a templated function takes T by value and you pass a function then T is a pointer to function. if you take T by reference, T will be function
Armen Tsirunyan