Eduard Rostomyan
Eduard Rostomyan

Reputation: 6546

How can I specialize a template class member function?

Consider the following sample:

    template <class T, class U>
class Test {
public:
    void f(){std::cout<<"f() not specized"<<std::endl;}
    void g(){std::cout<<"g() not specized"<<std::endl;}
    void h(){std::cout<<"h() not specized"<<std::endl;}

    //void g<long, double>(){}
};

Here I have en error on the commented code. I guess the only way achieve expected result is to spacialize the whole class. But in the class mentioned below, I cannot use the default behaviour of my original class (e. f() and h() functions).

template <>
class Test<long, double> {
public:
    void f(){std::cout<<"f() specized long, double"<<std::endl;}
};

So is there a way to spacialize the function in the original class??

Upvotes: 1

Views: 55

Answers (1)

Qaz
Qaz

Reputation: 61900

Sure, you can do it with a definition:

template<>
void Test<long, double>::f(){std::cout<<"f() specialized long, double"<<std::endl;}

Upvotes: 8

Related Questions