As a follow-up to this question: I need to decide in a class function like this:
template< typename T > bool Class::Fun <T*> ( T& variable ) {...}
whether T is a pointer or not.
In the question cited above the answer was to use partial template specialization. As far as I've found out this is not possible for class functions. Is this true? If so, is there another way of finding out if T is a pointer?
No need to specialize member function. In that answer used stand-alone structure. You're still free to use it in class member functions.
// stand-alone helper struct
template<typename T>
struct is_pointer { static const bool value = false; };
template<typename T>
struct is_pointer<T*> { static const bool value = true; };
// your class
class Class{
public:
template<typename T>
void Fun(T& variable) {
std::cout << "is it a pointer? " << is_pointer<T>::value << std::endl;
}
};
On the other hand, you could overload function:
class Class {
public:
template<typename T>
void Fun(T& variable) {
std::cout << "is it not a pointer! " << std::endl;
}
template<typename T>
void Fun(T*& variable) {
std::cout << "is it a pointer! " << std::endl;
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With