Q:

Delete value with generic type only if it's a pointer

Say you have a class such as:
template<typename T>
class Foo
{
public:
    T value;
public:
    Foo(const T& value)
    {
        this->value = value;
    }
public:
    ~Foo()
    {
        if(std::is_pointer<T>::value) delete value;
    }
}

Now, obviously the compiler will throw an error if T isn't a pointer. Example:
Foo<int> foo(42);

Is there a way to successfully check if T is a pointer, delete it if it is, without a compiler error?

A:

Use a specialized helper class, something along the following lines:
template<typename T> class delete_me {

public:

   static void destroy(T &value)
   {
   }
};

template<typename P>
class delete_me<P *> {

public:

   static void destroy(P *value)
   {
       delete value;
   }
};

And your destructor:
~Foo()
{
    delete_me<T>::destroy(value);
}

