mardi 5 février 2019

Can I get rid of template specialisation with std::enable_if?

I read about std::enable_if for:

function overloading based on arbitrary properties of type

So I was trying to overload class ctors via enable_if (like below) but i get error saying that enable_if cannot be used to disable declaration and this in both lines when I used std::enable_if :

#include <iostream>
#include <type_traits>
#include <typeinfo>


template <typename T>
class cls
{
public:

    cls (T a, typename std::enable_if< std::is_same<T, int>::value >::type  * Dummy = 0)
    {
        std::cout << "Ctor for int\n";
    }

    cls (T a, typename std::enable_if< std::is_same<T, char>::value >::type  * Dummy = 0)
    {
        std::cout << "Ctor for char\n";
    }
};

int main()
{

    cls a(10);
    cls b('x');

    return 0;
}

So is it possible to overload ctors using enbale_if.

Aucun commentaire:

Enregistrer un commentaire