Svalorzen
Svalorzen

Reputation: 5608

Using directive and Partial Specialization

I have a templatized interface class, with a couple of implemented methods and a couple of virtual ones.

I need to specialize it in order to modify the signature of some methods, but others would remain the same.

Is there a way to bring the methods that remain the same back from the original template, either via using directive, by directly calling back to them or in another way, or I must copy/paste every single method back into the specialization?

template <typename T>
struct X {
    void faa(T t) const { std::cout << t << '\n'; }
    void foo() const { std::cout << "foo\n"; }
};

template <>
struct X<void> {
    void faa() const { std::cout << "none\n"; }

    // Something along these lines
    // using X<T>::foo;
    // void foo() const { X<T>::foo(); }
};

Upvotes: 2

Views: 111

Answers (1)

iBug
iBug

Reputation: 37267

Seems so. You can't get the functions in X with different signatures using using directives. There is a better workaround than copying everything from the template to the specialization. You can use a "common base class".

template <typename T>
struct X_base {
    void foo() const { std::cout << "foo\n"; }
};

template <typename T>
struct X : public X_base<T> {
    void faa(T t) const { std::cout << t << '\n'; }
};

template <>
struct X<void> : public X_base<void> {
    void faa() const { std::cout << "none\n"; }
};

In this way, X<void>::foo acts just like X_base<void>::foo, while X<T>::faa and X<void>::faa do not interfere with each other.

Upvotes: 4

Related Questions