Template Specialization for T -> std::vector<T>

Off the top of my head, I usually get around it by using a one-member struct:

template <typename T>
struct pop_impl {
    static T pop(classname& x); // normal function
};

template <typename T>
struct pop_impl<std::vector<T>> {
    static std::vector<T> pop(classname& x); // specialized for std::vector<T>
};

template <typename T>
T classname::pop() { return pop_impl<T>::pop(*this); }

This answer was originally provided by Austin Salgat in the body of the question Template Specialization for T -> std::vector, (posted under the CC BY-SA 3.0 license), and has been moved here as an answer in order to adhere to the site's Q&A format.


Thanks to Piotr I ended up using tag dispatching. Below is the code for what I ended up doing,

// The public method that is accessed by class.push<std::vector<int>>(12);
template<class T>
void push(T data) {
    push(tag<T>(), data);
}

// The private method that calls the actual vector push for vector types
template<class T>
void push(tag<std::vector<T>>, std::vector<T> const& data_vector) {
    push_vector(data_vector);
}

// The actual implementation
template<class T>
void push_vector(std::vector<T> const& data_vector) {
// Actual implementation in here
}

Tags:

C++

Templates