Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specialization of template class method

Tags:

c++

templates

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).

like image 865
Mark Ingram Avatar asked Oct 13 '12 22:10

Mark Ingram


People also ask

What is a template specialization?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What is the specialty of a template function give example?

Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type.

What are the two specializations of i/o template classes in C ++?

What are the two specializations of I/O template classes in C++? Explanation: The I/O specialization is made with wide character and 8-bit characters.

Are template specializations inline?

An explicit specialization of a function template is inline only if it is declared with the inline specifier (or defined as deleted), it doesn't matter if the primary template is inline.


1 Answers

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> {
};
like image 167
JohnB Avatar answered Sep 19 '22 23:09

JohnB