AppleClang: ошибка компиляции оператора при перегрузке - PullRequest
0 голосов
/ 08 декабря 2018

Я реализовал перегрузку операторов следующим образом (см. Методы bool operator== и bool operator<):

#include "../data_types.h"

class OffsetValuePair {
private:
    unsigned_value offset;
    unsigned_value value;

public:
    OffsetValuePair(unsigned_value address, unsigned_value value) {
        this->offset = address;
        this->value = value;
    }

    bool operator==(const OffsetValuePair offsetValuePair) {
        return this->getOffset() == offsetValuePair.offset;
    }

    bool operator<(const OffsetValuePair offset_value_pair) {
        return this->getValue() < offset_value_pair.value;
    }

    unsigned_value getOffset() {
        return offset;
    }

    unsigned_value getValue() {
        return value;
    }
};

Я использую оператор для нахождения нижней границы, используя, будет использовать мой перегруженный оператор для меньшихthan (<):

const auto lower_bound_offset_value_pair = OffsetValuePair(0, 1234);
const auto lower_bound = std::lower_bound(pointer_map_sorted_by_value_.begin(),
                                                  pointer_map_sorted_by_value_.end(),
                                                  lower_bound_offset_value_pair);

Когда я компилирую код, я получаю следующую ошибку с AppleClang:

/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:719:71: error: invalid operands to binary expression ('const OffsetValuePair' and 'const OffsetValuePair')
    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
                                                                  ~~~ ^ ~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:4285:13: note: in instantiation of member function 'std::__1::__less<OffsetValuePair, OffsetValuePair>::operator()' requested here
        if (__comp(*__m, __value_))
            ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:4307:12: note: in instantiation of function template specialization 'std::__1::__lower_bound<std::__1::__less<OffsetValuePair, OffsetValuePair> &, std::__1::__wrap_iter<OffsetValuePair *>, OffsetValuePair>' requested here
    return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
           ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:4316:19: note: in instantiation of function template specialization 'std::__1::lower_bound<std::__1::__wrap_iter<OffsetValuePair *>, OffsetValuePair, std::__1::__less<OffsetValuePair, OffsetValuePair> >' requested here
    return _VSTD::lower_bound(__first, __last, __value_,
                  ^
/Users/bully/Desktop/PointerSearcher/src/pointer_search_objects/PointerSearcher.h:660:33: note: in instantiation of function template specialization 'std::__1::lower_bound<std::__1::__wrap_iter<OffsetValuePair *>, OffsetValuePair>' requested here
                const auto lower_bound = std::lower_bound(pointer_map_sorted_by_value_.begin(),
                                              ^
/Users/bully/Desktop/PointerSearcher/src/pointer_search_objects/OffsetValuePair.h:21:7: note: candidate function not viable: 'this' argument has type 'const OffsetValuePair', but method is not marked const
        bool operator<(const OffsetValuePair offset_value_pair) {
             ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/utility:572:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:702:1: note: candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1143:1: note: candidate template ignored: could not match 'move_iterator<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1512:1: note: candidate template ignored: could not match '__wrap_iter<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT_DEBUG
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1187:1: note: candidate template ignored: could not match 'tuple<type-parameter-0-0...>' against 'const OffsetValuePair'
operator<(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2920:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2978:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2987:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:4758:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:4823:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:4831:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT

Использование gcc и MSVCКод компилируется нормально, я получаю эту ошибку только на Mac OS X со стандартным компилятором платформы (AppleClang).

Я не понимаю, почему AppleClang не принимает код.Что с ним не так?

Я не могу объявить методы const, так как они используют this ссылочный вызов метода.Если я преобразовываю объект this во второй аргумент метода, я получаю еще одну ошибку, сообщающую, что сигнатура метода неверна: error: 'bool OffsetValuePair::operator<(OffsetValuePair, OffsetValuePair)' must have exactly one argument

1 Ответ

0 голосов
/ 08 декабря 2018

Неважно, я только что нашел решение:

bool operator==(const OffsetValuePair offsetValuePair) const {
    return this->offset == offsetValuePair.offset;
}

bool operator<(const OffsetValuePair offset_value_pair) const {
    return this->value < offset_value_pair.value;
}

Изменения должны были определить оба метода как const, как предлагается в сообщении об ошибке.Кроме того, при использовании эталонного метода this вызовы недопустимы из-за определения метода const, поэтому я заменил геттер прямым доступом к члену.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...