为什么SFINAE在这个例子中不起作用?

时间:2018-02-22 11:18:19

标签: c++ templates sfinae

https://ec2.amazonaws.com/?Action=DescribeRegions&AWSAccessKeyId=****&SignatureMethod=HmacSHA256&SignatureVersion=1&Version=2013-08-15

这里我给出错误,第一个Create函数不能推导出模板参数,但后来我评论了它,第二个重载工作正常。为什么SFINAE在第一次过载时不起作用?

1 个答案:

答案 0 :(得分:2)

您的方法不是模板,它是您的类,它是模板。你必须改变

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();
}
相关问题