#include
#include
#include
int main() {
std::pair x = std::make_pair(5, 4);
std::pair y = std::make_pair(5, 4);
if (x == y) {
// Compiler Error
}
if (operator==(x, y)) {
// Okay
}
return 0;
}
< /code>
Я получаю ошибку компилятора в первом случае. По сути, в нем говорится, что при вызове std :: weder :: operator == второй параметр имеет тип std :: pare . Но, очевидно, у y есть тип std :: pare .
Так почему компилятор делает неявное преобразование типа здесь? И почему вторая версия работает, когда я явно вызову оператор == ?
edit (сообщение об ошибке):
g++ -O2 -std=c++23 -Wall -Wextra -Werror -Wpedantic -g -fconcepts-diagnostics-depth=10 -c /path/to/main.cpp -o /path/to/main.o
In file included from /path/to/main.cpp:136:
/usr/include/c++/15.1.1/expected: In substitution of ‘template requires !(__is_expected) && requires(const _Tp& __t, const _Up& __u) {{__t == __u} -> decltype(auto) [requires std::convertible_to];} constexpr bool std::operator==(const expected&, const _Up&) [with _Up = std::pair]’:
/usr/include/c++/15.1.1/expected
1175 | { __t == __u } -> convertible_to;
| ~~~~^~~~~~
/path/to/main.cpp:144:11: required from here
144 | if (x == y) {
| ^
/usr/include/c++/15.1.1/expected
/usr/include/c++/15.1.1/expected
/usr/include/c++/15.1.1/expected
1174 | && requires (const _Tp& __t, const _Up& __u) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1175 | { __t == __u } -> convertible_to;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1176 | }
| ~
Подробнее здесь: https://stackoverflow.com/questions/796 ... convertion