Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one use enable_if for mutually exclusive non-member function templates?

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?

like image 529
CTMacUser Avatar asked Jun 02 '12 11:06

CTMacUser


1 Answers

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.

like image 148
Xeo Avatar answered Nov 14 '22 23:11

Xeo