5

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?

Community
  • 1
  • 1
fuenfundachtzig
  • 7,952
  • 13
  • 62
  • 87
  • 1
    @iammilind: You'll note that the question already linked to the question you've marked it a duplicate of and that it also was already stated there in what respect it is different. Please remove the "marked as duplicate". – fuenfundachtzig Mar 17 '16 at 15:11

3 Answers3

22

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;
 }
};
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
8

Take a look at boost::is_pointer.

moonshadow
  • 86,889
  • 7
  • 82
  • 122
3

Have a look at Boost.TypeTraits, together with Boost EnableIf.

gimpf
  • 4,503
  • 1
  • 27
  • 40