0
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.

1
  • Won't I need size_t N in the first template too so that the second one is considered as a partial specialization of the first? Commented Oct 10, 2010 at 19:19

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@There: No, as a matter of fact, I just got home 5 min ago. Was my answer helpful?
yes, thank you. Can't accept though. Have to wait 15 minutes.
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.