(e-mail address removed) a écrit :
If I have a template class with a member function:
template <typename T> class C {
void member ();
};
template <typename T> void C<T>::member () {
}
Is there some way to make a specialized implementation of member()
that is used when T is any pointer type?
Not directly. You would have to specialize the outer class.
The way is to use overload:
1. You have to have a predicate telling you whether or not a type is a
pointer:
template <typename T>
struct is_pointer
{
static const bool value=false;
};
template <typename T>
struct is_pointer<T*>
{
static const bool value=true;
};
2. And then an operator to transform this operation into a type:
template<typename T,bool b=is_pointer<T>::value >
struct is_pointer_type{};
3. And then, use overloading
template <typename T> struct C {
void member ()
{ return member(is_pointer_type<T>());}
void member(const is_pointer_type<T,true>&);
void member(const is_pointer_type<T,false>&);
};
You can make the distinction:
template <typename T> void C<T>::member(const is_pointer_type<T,false>&)
{
cout<<"Not a pointer"<<endl;
}
template <typename T> void C<T>::member (const is_pointer_type<T,true>&)
{
cout<<"Is a pointer"<<endl;
}
Normaly, the extra call and parameter are optimized out.