How to call generic template function in a specialization version

Viewed 3270

Have a problem about how to call the generic template version in a specialization version.

Here is the sample code. But the "vector::push_back(a)" calls itself recursively.

#include <iostream>
#include <vector>

using namespace std;

namespace std
{
        template<>
        void vector<int>::push_back(const int &a)
        {
                cout << "in push_back: " << a << endl;
                vector::push_back(a);               // Want to call generic version
        }
}

int main()
{
        vector<int> v;
        v.push_back(10);
        v.push_back(1);
        return 0;
}
3 Answers

You can simply extract the code into another template function:

    template<typename T>
    void baseF(T t) { ... }

    template<typename T>
    void F(T t) { baseF<T>(t); }

    template<>
    void F<int>(int t) { baseF<int>(t); }
Related