Specializing function templates outside class temp. definition - what is the correct way of doing t

Posted by LoudNPossiblyRight on Stack Overflow See other posts from Stack Overflow or by LoudNPossiblyRight
Published on 2010-06-08T14:10:04Z Indexed on 2010/06/08 14:12 UTC
Read the original article Hit count: 199

Filed under:
|

I am attempting to specialize a function template that is a member of a template class. The two of them have different template parameters. The template function specialization inside the temp. class definition is never called and the one func. spec. outside the class definition does not even compile.

Should i expect this to work in the first place, and if so, what do i have to change in this code to both compile and make it work correctly:

using VS2010

#include<iostream>
using namespace std;

template <typename T>
class klass{
public:
    template <typename U>  
    void func(const U &u){
        cout << "I AM A TEMPLATE FUNC" << endl;
    }

    //THIS NEVER GETS CALLED !!!
    template <>
    void klass<T>::func(const string &s){
        cout << "I AM A STRING SPECIALIST" << endl;
    }
};

//THIS SPECIALIZATION WILL NOT COMPILE !!!
template <typename T> template <>
void klass<T>::func(const double &s){
    cout << "I AM A DOUBLE SPECIALIST" << endl;
}

int main(){

    double d = 3.14159265;
    klass<int> k;
    k.func(1234567890);
    k.func("string");
    k.func(3.14159265);
    return 0;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates