Почему оператор == делает неявное преобразование типа?C++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Почему оператор == делает неявное преобразование типа?

Сообщение Anonymous »

Я не могу скомпилировать код, который сводится к следующему примеру: < /p>
#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:12: required by 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]’
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:1178:2: required by the constraints of ‘template 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&)’
/usr/include/c++/15.1.1/expected:1174:7: in requirements with ‘const _Tp& __t’, ‘const _Up& __u’ [with _Tp = int; _Up = std::pair]
/usr/include/c++/15.1.1/expected:1174:14: error: satisfaction of atomic constraint ‘requires(const _Tp& __t, const _Up& __u) {{__t == __u} -> decltype(auto) [requires std::convertible_to];} [with _Tp = _Tp; _Up = _Up]’ depends on itself
1174 | && requires (const _Tp& __t, const _Up& __u) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1175 | { __t == __u } -> convertible_to;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1176 | }
| ~



Подробнее здесь: https://stackoverflow.com/questions/796 ... convertion
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»