Clang принимает назначенную инициализацию, где GCC/MSVC отклоняет — ошибка компилятора или неверный C++?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Clang принимает назначенную инициализацию, где GCC/MSVC отклоняет — ошибка компилятора или неверный C++?

Сообщение Anonymous »

Clang компилирует следующий фрагмент кода C++20, но в то же время он отклоняется как GCC, так и MSVC.
Есть как минимум два варианта:
  • Код недопустим, и Clang не сообщает о соответствующей ошибке.
  • Код легален, и как GCC, так и MSVC содержат ошибку.
Какой именно? Или это совсем другое?
https://godbolt.org/z/qzc5zqb9W
template
struct B {
consteval B(T t) : m_t{ t } { };

template
operator B() const { return B(static_cast(m_t), typename B::private_ctor_tag{}); }

operator T() const { return m_t; }

private:
struct private_ctor_tag{};
B(T t, private_ctor_tag) : m_t{ t } { };

template
friend struct B;

T m_t;
};

struct A {
B b;
};

int main() {

constexpr B my_b{ 42 };

// okay on all compilers
A a0{ .b = my_b };

// okay only with clang. msvc and gcc give errors.
A a1{ .b{ my_b } };
A a2{ .b = { my_b } };

return 0;
}

Изменить: MSVC выдает следующий результат:
(32): error C2440: 'initializing': cannot convert from 'initializer list' to 'B'
(32): note: 'B::B': ambiguous call to overloaded function
(18): note: could be 'B::B(B &&)'
(18): note: or 'B::B(const B &)'
(3): note: or 'B::B(T)'
with
[
T=double
]
(32): note: while trying to match the argument list '(const B)'
(33): error C2440: 'initializing': cannot convert from 'initializer list' to 'B'
(33): note: 'B::B': ambiguous call to overloaded function
(18): note: could be 'B::B(B &&)'
(18): note: or 'B::B(const B &)'
(3): note: or 'B::B(T)'
with
[
T=double
]
(33): note: while trying to match the argument list '(const B)'

И GCC дает:
:32:22: error: conversion from '
' to 'B' is ambiguous
32 | A a1{ .b{ my_b } };
| ^
• there are 3 candidates
• candidate 1: 'consteval B::B(T) [with T = double]'
:3:15:
3 | consteval B(T t) : m_t{ t } { };
| ^
• candidate 2: 'constexpr B::B(const B&)'
:2:8:
2 | struct B {
| ^
• candidate 3: 'constexpr B::B(B&&)'
:33:25: error: conversion from '
' to 'B' is ambiguous
33 | A a2{ .b = { my_b } };
| ^
• there are 3 candidates
• candidate 1: 'consteval B::B(T) [with T = double]'
:3:15:
3 | consteval B(T t) : m_t{ t } { };
| ^
• candidate 2: 'constexpr B::B(const B&)'
:2:8:
2 | struct B {
| ^
• candidate 3: 'constexpr B::B(B&&)'```


Подробнее здесь: https://stackoverflow.com/questions/798 ... ler-bug-or
Ответить

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

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

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

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

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