Consider this simple type:
struct A {
A(int, int, int) {}
A(std::vector<int>) {}
};
///
A a({1,2,3});
The snippet above constitutes an ill-formed program, as overload resolution fails because {1, 2, 3}
can be used to initialize a temporary A
, and so the copy c'tor is viable as well for initializing a
.
I initially assumed that marking the first c'tor as explicit A(int, int, int)
could be used to resolve the issue. And Clang agrees. On the other hand, GCC still does not accept it, stating the same reason of two viable candidate c'tors.
To make matters worse, this is made even more confusing by another observation. If one was to use list initialization instead, i.e. A a{{1,2,3}};
, then suddenly both Clang and GCC accept the code with the original class definition unchanged (where nothing is explicit
).
Which compiler is being compliant in the case of A a({1,2,3});
?