Возможное изменение преобразования типа C ++ 17 и MSVC против GCC/Clang, кто прав?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Возможное изменение преобразования типа C ++ 17 и MSVC против GCC/Clang, кто прав?

Сообщение Anonymous »


struct B;

struct A {
operator B() const;
};

struct B {};

void f() {
(void)B(A{});
}
< /code>
Это работает с GCC, Clang и MSVC, как и ожидалось.struct B;

struct A {
operator B() const;
operator int() const;
};

struct B {
B(int);
};

void f() {
(void)B(A{});
}

https://godbolt.org/z/g7fe998t6
>> Ошибка MSVC:
(13): error C2440: '': cannot convert from 'A' to 'B'
(13): note: 'B::B': ambiguous call to overloaded function
(10): note: could be 'B::B(B &&)'
(10): note: or 'B::B(const B &)'
(9): note: or 'B::B(int)'
(13): note: while trying to match the argument list '(A)'
< /code>
Кто здесь: GCC и Clang или MSVC? < /p>
Я наткнулся на это в контексте C ++ 20. Для C ++ 17/20/23 это то же самое, но для C ++ 11/14 я получаю аналогичную ошибку с GCC и менее похожей ошибкой с Clang. Таким образом, это может быть изменение правила в C ++ 17. < /p>
c ++ 14 gcc ошибка: < /p>
:13:16: error: call of overloaded 'B(A)' is ambiguous
13 | (void)B(A{});
| ^
:9:5: note: candidate: 'B::B(int)'
9 | B(int);
| ^
:8:8: note: candidate: 'constexpr B::B(const B&)'
8 | struct B {
| ^
:8:8: note: candidate: 'constexpr B::B(B&&)'
< /code>
c ++ 14 Ошибка Clang: < /p>
:13:11: error: ambiguous conversion for functional-style cast from 'A' to 'B'
13 | (void)B(A{});
| ^~~~~~
:8:8: note: candidate constructor (the implicit copy constructor)
8 | struct B {
| ^
:8:8: note: candidate constructor (the implicit move constructor)
:9:5: note: candidate constructor
9 | B(int);
| ^


Подробнее здесь: https://stackoverflow.com/questions/795 ... whos-right
Ответить

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

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

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

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

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