How can you specialize a non-template member function of a class template?
Asked Answered
R

2

6

Suppose I have the following class:

template <typename T>
class MyClass
{
public:
    void SetValue(const T &value) { m_value = value; }

private:
    T m_value;
};

How can I write a specialized version of the function, for T=float (or any other type)?

Note: A simple overload won't suffice because I only want the function to be available for T=float (i.e. MyClass::SetValue(float) doesn't make any sense in this instance).

Ravi answered 13/10, 2012 at 22:4 Comment(0)
C
10
template <typename T>
class MyClass {
private:
    T m_value;

private:
    template<typename U>
    void doSetValue (const U & value) {
        std::cout << "template called" << std::endl;
        m_value = value;
    }

    void doSetValue (float value) {
        std::cout << "float called" << std::endl;
    }

public:
    void SetValue(const T &value) { doSetValue (value); }

};

or (partial template specialization):

template <typename T>
class MyClass
{
private:
    T m_value;

public:
    void SetValue(const T &value);

};

template<typename T>
void MyClass<T>::SetValue (const T & value) {
    std::cout << "template called" << std::endl;
    m_value = value;
}

template<>
void MyClass<float>::SetValue (const float & value) {
    std::cout << "float called" << std::endl;
}

or, if you want the functions to have different signatures

template<typename T>
class Helper {
protected:
    T m_value;
    ~Helper () { }

public:
    void SetValue(const T &value) {
        std::cout << "template called" << std::endl;
        m_value = value;
    }
};

template<>
class Helper<float> {
protected:
    float m_value;
    ~Helper () { }

public:
    void SetValue(float value) {
        std::cout << "float called" << std::endl;
    }
};

template <typename T>
class MyClass : public Helper<T> {
};
Captivity answered 14/10, 2012 at 8:14 Comment(2)
Your partial template specialization answer hit the nail on the head! Just what I was looking for, thanks.Ravi
Actually, it's explicit template specialization in your second example: You're providing all 1 out of 1 template argument(s), if there'd been, you'd have needed to provide 2. Another point is that these specialized functions apparently need to be in a separate compilation unit (not the header file) if you put the template into a header file and include it twice.Rambling
H
3

Sure you can. It's just that it should be an overload :)

template <typename T>
class MyClass
{
public:
    template<class U> 
    void SetValue(const U &value) { m_value = value; }
    void SetValue(float value) {do special stuff} 
private:
    T m_value;
};

int main() 
{
     MyClass<int> mc;
     mc.SetValue(3.4); // the template member with U=double will be called
     mc.SetValue(3.4f); // the special function that takes float will be called

     MyClass<float> mcf; //compiles OK. If the SetValue were not a template, 
     // this would result in redefinition (because the two SetValue functions
     // would be the same   
}
Hitt answered 13/10, 2012 at 22:11 Comment(1)
But I only want the float overload to be available when T=float. Otherwise, you could have MyClass<int>::SetValue(float) which wouldn't make any sense.Ravi

© 2022 - 2024 — McMap. All rights reserved.