dimanche 13 février 2022

enable_if not able to speciialize a function for more than two cases

I have the following code to specialize the function start for the particular vehicle type in C++11:

template< typename Vehicle>

struct s{
    template< typename T = void>
    typename enable_if<is_same<Vehicle, Car>::value, T>::type start() {
        // body
    }

    template< typename T = void>
    typename enable_if<is_same<Vehicle, Bike>::value, T>::type start() {
       // body
    }

    template< typename T = void>
    typename enable_if<is_same<Vehicle, Scooter>::value, T>::type start() {
       // body
    }
};
int main() {
    s<Car>::start();
}

When I compile this it throws an error: multiple overloads of 'start' instantiate to the same signature

The thing is when I remove one of the specializations(say the scooter type), it compiles happily. Why is this happening and how can I resolve this issue ? Any leads would be appreciated .

Aucun commentaire:

Enregistrer un commentaire