0
#include <iostream>
#include <type_traits>

class CL
{
    public:
    CL(){}
    CL( int ) = delete;
};

template < class T >
class Creator
{
public:
    template< std::enable_if_t< std::is_constructible<T, int>::value, int > = 0 >
    static T* Create( int arg ) // 1
    {
        return new T( arg );
    }
    template< std::enable_if_t< std::is_default_constructible<T>::value && !std::is_constructible<T, int>::value, int > = 0 >
    static T* Create( int arg ) // 2
    {
        return new T();
    }
};

int main()
{
    Creator<CL>::Create( 2 );
}

Here I give error that the first Create function can't deduce template argument, but then I commented it, the second overload is works fine. Why SFINAE doesn't work on first overload?

Denis
  • 2,786
  • 1
  • 14
  • 29

1 Answers1

2

Your method is not template, it is your class which is template. You had to change

template<typename U = T,
         std::enable_if_t< std::is_constructible<U, int>::value, int > = 0 >
static T* Create( int arg ) // 1
{
    return new T( arg );
}

template<typename U = T,
         std::enable_if_t< std::is_default_constructible<U>::value
                          && !std::is_constructible<U, int>::value, int > = 0 >
static T* Create( int arg ) // 2
{
    return new T();
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302