6

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?

Xeo
  • 129,499
  • 52
  • 291
  • 397
CTMacUser
  • 1,996
  • 1
  • 16
  • 27

1 Answers1

11

Interestingly, a certain fellow here on SO wrote a blogpost just a short time ago, showing a nice C++11-style SFINAE technique that easily allows overloaded functions. The technique and explanation are provided here.

In short, your code fails because both templates, when parsed the first time and when non-dependant declarations are resolved, are exactly the same, type-wise. As with default function arguments, default template arguments are only substituted when the function is actually called. This is what both templates look like to the compiler at the point of declaration:

template<class T, unsigned Lt, class U, unsigned Lu, class Enable>
auto operator==(MyType<T,Lt> const& l, MyType<U,Lu> const& r);

The following code should achieve what you want:

namespace detail{
enum class enabler{};
}

template<bool B, class T = detail::enabler>
using EnableIf = typename std::enable_if<B, T>::type;

template < typename T, unsigned Lt, typename U, unsigned Lu, EnableIf<(Lt < Lu)>...>
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, EnableIf<(Lt > Lu)>...>
auto  operator ==( MyType<T,Lt> const &l, MyType<U,Lu> const &r )
 -> decltype( std::declval<T>() == std::declval<U>() )
{ /*...*/ }

One question remains, however... what should happen if Lt == Lu? In that case, neither overload is viable.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • Wow! My mind got blown. I had thought about the multi-condition (easy enough with variadic), but... combining so many technics and managing to hit such a syntactical sweet spot. Amazing. Thanks for sharing the article. – Matthieu M. Jun 02 '12 at 13:38
  • The version of `operator ==` in the first block covers the case when the class template's second parameters are equal. The two versions I'm asking about are in addition to the first version, and are not replacing it. – CTMacUser Jun 03 '12 at 02:24
  • That is a *very* interesting article. For my code, I used `typename std::enable_if<(Lt < Lu)>::type...` as the final template parameter. My compiler, GCC-4.7 (32-bit PowerPC, from MacPorts), accepted that parameter, even though it's technically `void...` (an incomplete type) and the article said that `void` couldn't be used! I was planning to use `std::nullptr_t` or `int` since I didn't want to add a throwaway type to library code (but, never mind). – CTMacUser Jun 03 '12 at 02:31
  • @CTMacUser yeah, there's no big problem with using `int` or `std::nullptr_t`. I just like to be extra cautious :) – R. Martinho Fernandes Jun 03 '12 at 17:13