C++ template specialisation for abstract base class

I want to write a Settings class to load and store settings of various types:

template <typename T>
class Setting {
    std::string m_name;
    T m_defaultValue;

    T load() {
        if(backend.contains(m_name))
            return backend.load<T>(m_name);
        return m_defaultValue;
    }

    void save(const T &value) {
        backend.save(m_name, value);
    }
}

The backend has implementaions for some basic types, but not for my own. So I need template specializations for my types. My idea is to create a Serializable class and derive my classes from it:

class Serializable {
public:
    virtual std::string serialize() const = 0;
    virtual void deserialize(const std::string &data) = 0;
}

Now I need a template specialization for load and save in the Setting class to use the serialize and deserialize functions in Serializable:

template<>
void Setting<Serializable>::load()
{
    if(backend.contains(m_name)) {
        Serializable s;
        s.deserialize(backend.load<std::string>(m_name);
        return s;
    }
    return m_defaultValue;
}

template<>
void Setting<Serializable>::save(const Serializable &value)
{
    backend.save(m_name, value.serialize());
}

Obviously this doesn’t work because in Setting<Serializable>::load() I cannot instantiate Serializable s. But even if I leave out load for the time being, it does not work because it cannot declare field ‘Setting<Serializable>::m_defaultValue’ to be of abstract type ‘Serializable’.

Is there a way to write a different implementation for all subtypes of Serializable but still use a template parameter T?

Source: Windows Questions C++

LEAVE A COMMENT