ARTICLE AD BOX
I stumbled upon this code:
struct type1 { int a; }; struct type2 { int a; type1 agg; int b; }; struct deducer { operator type1() { return type1{}; } }; auto main() -> int { type2 a{0, deducer{}, 0}; // fine type2 b(0, deducer{}, 0); // error on msvc }https://godbolt.org/z/5d5ao6coa
Clang and GCC compiles it correctly, but MSVC don't.
It seems to me that MSVC is incorrect since the initialization with parenthesis should be almost equivalent to calling constructor function.
I attempted to find the relevant paragraph from the standard, but I'm not exactly sure of the validity.
From [dcl.init]/16.6.2.2:
Otherwise, if no constructor is viable, the destination type is an aggregate class, and the initializer is a parenthesized expression-list, the object is initialized as follows. Let e1, …, en be the elements of the aggregate ([dcl.init.aggr]). Let x1, …, xk be the elements of the expression-list. If k is greater than n, the program is ill-formed. The element ei is copy-initialized with xi for 1 ≤ i ≤ k. The remaining elements are initialized with their default member initializers, if any, and otherwise are value-initialized. For each 1 ≤ i < j ≤ n, every value computation and side effect associated with the initialization of ei is sequenced before those associated with the initialization of ej.
From that rule, we then get to [dcl.init]/16.6.3
Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversions that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in [over.match.copy], and the best one is chosen through overload resolution ([over.match]). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call is a prvalue of the cv-unqualified version of the destination type whose result object is initialized by the constructor. The call is used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization.
That would imply user defined conversion should be allowed.
Now the questions:
Is MSVC right, or is it GCC and Clang that are right?
Are the paragraph from the standard I found the right one to describe why user defined conversion should be allowed in parenthesized initialization of aggregates?
