Can I partial compile a template function in C++

Viewed 1389

I have a function to determine whether a template type is pointer.

template<class T>
struct is_pointer_struct { static const bool value = false; };

template<class T>
struct is_pointer_struct<T*> { static const bool value = true; };

template<class T>
bool is_pointer(T &var) {
    return is_pointer_struct<T>::value;
}

And I have a initialize function.

template<class T>
void initialize(T &val) {
    if (is_pointer(val))
        val = NULL;
    else
        val = T();
}

Obviously, when T is string, this code can't be compiled. Is there a way that compile val = NULL when T is pointer type and compile val = T() when T is not a pointer type?

3 Answers
Related