Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template specialization for interface

Tags:

c++

Why won't the compiler select the Interface template when running the following code? Are additional declarations / hints needed or won't this work in general?

I'm just curious if this is actually possible.

class Interface {
    public :
       virtual void Method() = 0;
       virtual ~Interface() { }
};

class Derived : Interface {
    public : 
       void Method() {
            cout<<"Interface method"<<endl;
       }
};

template<typename T> 
struct Selector {
    static void Select(T& o) {
        cout<<"Generic method"<<endl;
    }
};

template<> 
struct Selector<Interface> {
    static void Select(Interface& o) {
        o.Method();
    }
};

int i;
Selector<int>::Select(i)       // prints out "Generic method" -> ok
Derived d;
Selector<Derived>::Select(d);  // prints out "Generic method" -> wrong
                               // should be "Interface method"
like image 262
andwffn Avatar asked Dec 21 '22 13:12

andwffn


2 Answers

Try this (and #include <type_traits>):

template <typename T, typename = void>
struct Selector
{
    static void Select(T & o)
    {
        std::cout << "Generic method" << std::endl;
    }
};

template <typename T>
struct Selector<T,
           typename std::enable_if<std::is_base_of<Interface, T>::value>::type>
{
    static void Select(Interface & o)
    {
        o.Method();
    }
};

It turns out that enable_if combined with defaulted template arguments can be used to guide partial specialisations.

like image 155
Kerrek SB Avatar answered Dec 24 '22 01:12

Kerrek SB


The compiler will select the version of a function that is the closest match. A function that takes the exact type for a parameter always wins over one that requires a conversion. In this case the template function is an exact match, since it matches anything; the Interface specialization would require the conversion of the parameter from a Derived to an Interface.

like image 23
Mark Ransom Avatar answered Dec 24 '22 02:12

Mark Ransom