我正在尝试编写非成员运算符函数模板,例如:
#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>() )
{ /*...*/ }
但是当我尝试处理何时l
并r
有不同的长度时:
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>() )
{ /*...*/ }
我得到模棱两可的错误。我试过类似的东西:
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>() )
{ /*...*/ }
我已经阅读过(在 SO 上)来解决成员函数模板的此类问题。(有时,受访者将成员函数更改为成员函数模板以启用此功能。)但错误对我来说并没有改变。我必须切换到enable_if
返回类型吗?
哦,当两个元素类型无法比较时,返回类型表达式应该排除这个运算符。它真的会起作用吗?它也与将enable_if
周围放在那里兼容吗?