Home SFINAE works as return type, but not parameter type

# SFINAE works as return type, but not parameter type

user12248
1#
user12248 Published in September 19, 2018, 9:07 am

I was trying to use enable_if to avoid duplicating code. It works fine as long as placed in the return type, but not if it's in the parameters. Before this gets closed as a dup of this, the error I'm getting is not a redefinition, but a "no matching function for call." Here's my MCVE (not so "C", or so "M" for that matter) using VS2015 and g++ 7.2.0 (mingw):

#include <cmath>
#include <array>
#include <algorithm>

template <typename T, size_t M, size_t N>
class Matrix
{
public:
static const size_t ROWS = M;
static const size_t COLS = N;
typedef T SCALAR;

SCALAR operator[](const size_t index) const
{
static_assert((COLS == 1 || ROWS == 1), "operator[] is only for vectors (single row or column).");
return m_elements.at(index);
}

SCALAR& operator[](const size_t index)
{
static_assert((COLS == 1 || ROWS == 1), "operator[] is only for vectors (single row or column).");
return m_elements.at(index);
}

std::array<T, M * N> m_elements;
};

template <typename T, size_t N, size_t M>
static inline T Length(
const Matrix<typename std::enable_if<(M == 1 || N == 1), T>::type, N, M> & input)
{
T value = 0;
for (size_t i = 0; i < std::max(N, M); ++i)
{
value += (input[i] * input[i]);
}
return std::sqrt(value);
}

template <typename T, size_t M, size_t N>
static inline
Matrix<typename std::enable_if<(M == 3 && N == 1) || (M == 1 && N == 3), T>::type , M, N>
CrossProduct(const Matrix<T, M, N> & a, const Matrix<T, M, N> & b)
{
Matrix<T, M, N> result;
result[0] = a[1] * b[2] - a[2] * b[1];
result[1] = a[2] * b[0] - a[0] * b[2];
result[2] = a[0] * b[1] - a[1] * b[0];
return result;
}

Matrix<double, 1, 1> m11;
Matrix<double, 3, 1> m31;
Matrix<double, 1, 3> m13;
Matrix<double, 3, 3> m33;

auto l0 = Length(m11);  // Should work, but doesn't: no matching function for call to 'Length(Matrix<double, 1, 1>&)'
auto l1 = Length(m31);  // Should work, but doesn't: no matching function for call to 'Length(Matrix<double, 3, 1>&)'
auto l2 = Length(m13);  // Should work, but doesn't: no matching function for call to 'Length(Matrix<double, 1, 3>&)'
//auto l3 = Length(m33);    // Shouldn't work, and doesn't: no matching function for call to 'Length(Matrix<double, 3, 3>&)'

auto v1 = CrossProduct(m13, m13); //Works, as expected
//auto v2 = CrossProduct(m11, m11); // As expected: enable_if.cpp:71:32: error: no matching function for
// call to 'CrossProduct(Matrix<double, 1, 1>&, Matrix<double, 1, 1>&)'


If I change the signature of Length to

static inline typename std::enable_if<(M == 1 || N == 1), T>::type \
Length(const math::Matrix<T, N, M> & input)


it works fine. But the error it gives me seems to indicate that it was able to determine the correct signature (e.g. Length(Matrix<double, 3, 1>&)).

Why is the compiler unable to find a matching function if the enable_if is in the parameter list, but is able to if it's in the return type?

• you may take a look at what-is-a-nondeduced-context? – Massimiliano Janes Jan 10 at 13:30
• @MassimilianoJanes Why are the ints not deducible? Based on the error, I would think that the compiler deduced it very well. – Avi Ginsburg Jan 10 at 13:32
• the int's are deducible, T is not, because it appears in a non-deduced context (ie std::enable_if<>::type) – Massimiliano Janes Jan 10 at 13:33
• Is this what you want? ideone.com/2h0ZHl – Killzone Kid Jan 10 at 13:44
• @KillzoneKid Yes, that works as well (so does template <typename T, size_t N, size_t M, typename Dummy = typename std::enable_if<(M == 1 || N == 1), T>::type>). But if my example in the parenthesis works, I don't see why it doesn't when in the parameter list. In both cases the compiler had to deduce the type of T. – Avi Ginsburg Jan 10 at 13:47