I have this minimal working example:
class A {
public:
A(int i) {}
A(bool b) {}
};
class B {
public:
operator int() { return 1; };
operator bool() { return false; };
operator A() { return A(1); }; // I added this conversion operator to remove ambiguity
};
struct S {
A a;
};
int main() {
B b;
A a{b}; // this works
S s1{.a = b}; // this also works
// error: conversion from '<brace-enclosed initializer list>' to 'A' is ambiguous
S s2{.a{b}}; // this does not work
return 0;
}
I added the conversion operator operator A() { return A(1); }; to class B to have an exact recipe how to convert B to A. This prevents compilation errors in the two lines
A a{b}; // this works
S s1{.a = b}; // this also works
But the line
S s2{.a{b}}; // this does not work
still does not compile. Why is that?
I use g++ (GCC) 14.2.0.
EDIT:
I tried a different compiler. In
clang version 21.0.0git (https://github.com/llvm/llvm-project.git 5c3a99760274a06f8cb7e7247ce69c2fde5fbf2a) it compiles without any errors. Is this a gcc bug?
clang main.cpp -std=c++20(no error) vsg++ main.cpp -std=c++20(error: conversion from '<brace-enclosed initializer list>' to 'A' is ambiguous). So I think since both compilers are new, they usec++20per default.S s3{ b };works. Which points to it being a bug in GCC regarding designated initializers.