Reputation: 24675
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.
Upvotes: 0
Views: 209
Reputation: 132994
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.
Upvotes: 5