I am currently trying to write some flexible compile time mathematics library and just came across a substitution failure that I can’t manage to get rid of. Here is the problem:
First of all, I’m writing a rational class, I’ll put the only part that is needed.
template<typename T>
class rational
{
static_assert(std::is_integral<T>::value, "Can only contain integral values.");
public:
constexpr rational(T numerator, T denominator);
private:
T _numerator;
T _denominator;
};
And to allow the library to be flexible, I was trying to make a heavy use of SFINAE in order to restrict operator function calls to only rational-rational, rational-integral and integral-rational, but which would work whatever the integral and the underlying type of integral is. Here are the function declarations for operator+ for example:
template<typename T, typename U>
constexpr
rational<typename std::common_type<T, U>::type>
operator+(const rational<T>& lhs, const rational<U>& rhs);
template<typename T, typename U>
constexpr
typename std::enable_if<std::is_integral<U>::value, rational<typename std::common_type<T, U>::type>>::type
operator+(const rational<T>& lhs, const U& rhs);
template<typename T, typename U>
constexpr
typename std::enable_if<std::is_integral<T>::value, rational<typename std::common_type<T, U>::type>>::type
operator+(const T& lhs, const rational<U> rhs);
And here is a faulty segment of code. It does not crash because of the static_assert but presumably because of substitution failure:
constexpr auto r1 = rational<int>(1, 2);
constexpr auto r2 = rational<int>(2, 4);
static_assert(r1 + r2 == rational<int>(1, 1), "");
The error is the following (I only kept the errors without the surrounding blabla):
... required by substitution of 'template<class T, class U> constexpr typename std::enable_if<std::is_integral<T>::value, smath::rational<typename std::common_type<_Tp, _Up>::type> >::type smath::operator+(const T&, smath::rational<U>) [with T = smath::rational<int>; U = int]'
... required from here
... error: operands to ?: have different types 'smath::rational<int>' and 'int'
... required by substitution of 'template<class T, class U> constexpr typename std::enable_if<std::is_integral<U>::value, smath::rational<typename std::common_type<_Tp, _Up>::type> >::type smath::operator+(const smath::rational<T>&, const U&) [with T = int; U = smath::rational<int>]'
... required from here
... error: operands to ?: have different types 'int' and 'smath::rational<int>'
My guess was that g++ would chose the first template function that works with two rational numbers and would be ok with it. However, it seems that it still tries to apply the last two functions and fails while trying to do so. That I can’t understand. Some help would be welcomed 🙂
EDIT: It seems that having the rational constructor explicit resolves the problem, which is great. However, I’m still interested in knowing why the substitution failed that hard.
The problem is the type passed to
std::enable_if<whatever, T>. Even though the substitution would fail, the argument needs to be reasonable but it isn’t. So, the use oftypename std::common_type<T, U>::typedoesn’t work if there is no such type for the potentially evaluated types. You’d need something else. What works is to create the substitution failure disabling the mixed integer/rational overloads in the template argument list:Right now I’m not entirely sure if this is a work-around to a gcc problem or if it necessary to do it that way.