I'm trying to write non-member operator function templates like:
#include <utility>
template < typename T, unsigned L >
class MyType;
template < typename T, typename U, unsigned L >
auto operator ==( MyType<T,L> const &l, MyType<U,L> const &r )
-> decltype( std::declval<T>() == std::declval<U>() )
{ /*...*/ }
But when I try to handle when l
and r
have different lengths:
template < typename T, unsigned Lt, typename U, unsigned Lu, class Enable = typename std::enable_if<(Lt < Lu)>::type >
auto operator ==( MyType<T,Lt> const &l, MyType<U,Lu> const &r )
-> decltype( std::declval<T>() == std::declval<U>() )
{ /*...*/ }
template < typename T, unsigned Lt, typename U, unsigned Lu, class Enable = typename std::enable_if<(Lt > Lu)>::type >
auto operator ==( MyType<T,Lt> const &l, MyType<U,Lu> const &r )
-> decltype( std::declval<T>() == std::declval<U>() )
{ /*...*/ }
I get ambiguity errors. I tried something like:
template < typename T, unsigned Lt, typename U, unsigned Lu, bool B = (Lt < Lu), class Enable = typename std::enable_if<B>::type >
auto operator ==( MyType<T,Lt> const &l, MyType<U,Lu> const &r )
-> decltype( std::declval<T>() == std::declval<U>() )
{ /*...*/ }
template < typename T, unsigned Lt, typename U, unsigned Lu, bool B = (Lt > Lu), class Enable = typename std::enable_if<B>::type >
auto operator ==( MyType<T,Lt> const &l, MyType<U,Lu> const &r )
-> decltype( std::declval<T>() == std::declval<U>() )
{ /*...*/ }
which I've read (here on S.O.) to solve problems like this for member function templates. (Sometimes, the respondents changed a member function to a member function template to enable this.) But the errors don't change for me. Do I have to switch to putting enable_if
into the return type?
Oh, the return type expression is supposed to exclude this operator when the two element types can't be compared. Will it actually work? Is it compatible with putting the enable_if
around there too?