Clang akzeptiert eine bestimmte Initialisierung, bei der GCC/MSVC sie ablehnt – Compilerfehler oder ungültiges C++?C++

Programme in C++. Entwicklerforum
Anonymous
 Clang akzeptiert eine bestimmte Initialisierung, bei der GCC/MSVC sie ablehnt – Compilerfehler oder ungültiges C++?

Post by Anonymous »

Clang kompiliert das folgende C++20-Code-Snippet, während es gleichzeitig von GCC und MSVC abgelehnt wird.
Es gibt mindestens zwei Optionen:
  • Der Code ist illegal und Clang meldet keinen entsprechenden Fehler.
  • Der Code ist legal und sowohl GCC als auch MSVC haben einen Fehler.
Welches ist es? Oder ist es etwas ganz anderes?
https://godbolt.org/z/qzc5zqb9W

Code: Select all

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;
}
Bearbeiten: MSVC liefert die folgende Ausgabe:

Code: Select all

(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)'
Und GCC gibt:

Code: Select all

: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&&)'```

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post