dimanche 11 avril 2021

How do I fix an Invalid Comparator error?

MSVS 16.9.3

Win7-64

I get an invalid comparator error on the second execution of my sort comparator passed to the C++ Sort function provided in <algorithm>. I don't understand why I am getting this error! The sort routine call is:

sort(sorted.begin(), sorted.end(), remsort);

These are the three versions of remsort that I used:

Version 1: always works:

bool HOAReports::remsort(ADI::RDA_Asset* lhs, ADI::RDA_Asset* rhs) { 
   return (lhs->getRem() < rhs->getRem());
};

Version 2: works on the first call to remsort by the sort routine, fails on the second:

bool HOAReports::remsort(ADI::RDA_Asset* lhs, ADI::RDA_Asset* rhs) { 
   return (  (lhs->getRem() < rhs->getRem())
          || ((lhs->getCatName()).compare(rhs->getCatName()) < 0)
          || ((lhs->getRDAName()).compare(rhs->getRDAName()) < 0)
          };

Version 3: works on the first call to remsort by the sort routine, fails on the second:

bool HOAReports::remsort(ADI::RDA_Asset* lhs, ADI::RDA_Asset* rhs) {
   bool return_value = (  (lhs->getRem() < rhs->getRem())
                      || ((lhs->getCatName()).compare(rhs->getCatName()) < 0)
                      || ((lhs->getRDAName()).compare(rhs->getRDAName()) < 0)
                       ); 
   return return_value;
};

Version 2/3 have the same functionality. On the first and second call to remsort only ((lhs->getRem() < rhs->getRem()) is executed and the return_value is true. Looking at the failed assertion, it looks like the assertion is checked on both calls but fails on the second.

The MSVS code which fails is:

// FUNCTION TEMPLATE _Debug_lt_pred
template <class _Pr, class _Ty1, class _Ty2,
    enable_if_t<is_same_v<_Remove_cvref_t<_Ty1>, _Remove_cvref_t<_Ty2>>, int> = 0>
constexpr bool _Debug_lt_pred(_Pr&& _Pred, _Ty1&& _Left, _Ty2&& _Right) noexcept(
    noexcept(_Pred(_Left, _Right)) && noexcept(_Pred(_Right, _Left))) {
    // test if _Pred(_Left, _Right) and _Pred is strict weak ordering, when the arguments are the cv-same-type
    const auto _Result = static_cast<bool>(_Pred(_Left, _Right));
    if (_Result) {
        _STL_VERIFY(!_Pred(_Right, _Left), "invalid comparator");
    }

    return _Result;
}

Aucun commentaire:

Enregistrer un commentaire