I am implementing some matrix operations for my templated matrix<T>
and matrix_view<T>
.
At first I had
template <typename T, typename U>
matrix<common_type_t<T, U>> operator+(matrix_view<T>, matrix_view<U>);
and expected it to work also for matrix
matrix<int> a, b;
auto c = a + b;
because there is a converting constructor from matrix<T>
to matrix_view<T>
.
But it didn't work, because of the "Type deduction does not consider implicit conversions" rule.
So I added another overload
template <typename T, typename U>
matrix<common_type_t<T, U>> operator+(matrix<T>, matrix<U>);
But the story is still not over, for operator+
between matrix<T>
and matrix_view<T>
, I need to define another two overloads to make them work.
Now I wonder if I have to define all four overloads for every matrix operation. That's a lot of boilerplate code. And indeed the number of overloads needed grows exponentially with the number of types involved, if I am to add some other types in the future.
Is there any solution to this problem? Preferably only requiring me to define the first overload.