@@ -4200,7 +4200,7 @@ static OverloadingResult ResolveConstructorOverload(
42004200// / \param IsListInit Is this list-initialization?
42014201// / \param IsInitListCopy Is this non-list-initialization resulting from a
42024202// / list-initialization from {x} where x is the same
4203- // / type as the entity?
4203+ // / aggregate type as the entity?
42044204static void TryConstructorInitialization (Sema &S,
42054205 const InitializedEntity &Entity,
42064206 const InitializationKind &Kind,
@@ -4230,6 +4230,14 @@ static void TryConstructorInitialization(Sema &S,
42304230 Entity.getKind () !=
42314231 InitializedEntity::EK_LambdaToBlockConversionBlockElement);
42324232
4233+ bool CopyElisionPossible = false ;
4234+ auto ElideConstructor = [&] {
4235+ // Convert qualifications if necessary.
4236+ Sequence.AddQualificationConversionStep (DestType, VK_PRValue);
4237+ if (ILE)
4238+ Sequence.RewrapReferenceInitList (DestType, ILE);
4239+ };
4240+
42334241 // C++17 [dcl.init]p17:
42344242 // - If the initializer expression is a prvalue and the cv-unqualified
42354243 // version of the source type is the same class as the class of the
@@ -4242,11 +4250,17 @@ static void TryConstructorInitialization(Sema &S,
42424250 if (S.getLangOpts ().CPlusPlus17 && !RequireActualConstructor &&
42434251 UnwrappedArgs.size () == 1 && UnwrappedArgs[0 ]->isPRValue () &&
42444252 S.Context .hasSameUnqualifiedType (UnwrappedArgs[0 ]->getType (), DestType)) {
4245- // Convert qualifications if necessary.
4246- Sequence.AddQualificationConversionStep (DestType, VK_PRValue);
4247- if (ILE)
4248- Sequence.RewrapReferenceInitList (DestType, ILE);
4249- return ;
4253+ if (ILE && !DestType->isAggregateType ()) {
4254+ // CWG2311: T{ prvalue_of_type_T } is not eligible for copy elision
4255+ // Make this an elision if this won't call an initializer-list
4256+ // constructor. (Always on an aggregate type or check constructors first.)
4257+ assert (!IsInitListCopy &&
4258+ " IsInitListCopy only possible with aggregate types" );
4259+ CopyElisionPossible = true ;
4260+ } else {
4261+ ElideConstructor ();
4262+ return ;
4263+ }
42504264 }
42514265
42524266 const RecordType *DestRecordType = DestType->getAs <RecordType>();
@@ -4291,6 +4305,12 @@ static void TryConstructorInitialization(Sema &S,
42914305 S, Kind.getLocation (), Args, CandidateSet, DestType, Ctors, Best,
42924306 CopyInitialization, AllowExplicit,
42934307 /* OnlyListConstructors=*/ true , IsListInit, RequireActualConstructor);
4308+
4309+ if (CopyElisionPossible && Result == OR_No_Viable_Function) {
4310+ // No initializer list candidate
4311+ ElideConstructor ();
4312+ return ;
4313+ }
42944314 }
42954315
42964316 // C++11 [over.match.list]p1:
@@ -4572,9 +4592,9 @@ static void TryListInitialization(Sema &S,
45724592 return ;
45734593 }
45744594
4575- // C++11 [dcl.init.list]p3, per DR1467:
4576- // - If T is a class type and the initializer list has a single element of
4577- // type cv U, where U is T or a class derived from T, the object is
4595+ // C++11 [dcl.init.list]p3, per DR1467 and DR2137 :
4596+ // - If T is an aggregate class and the initializer list has a single element
4597+ // of type cv U, where U is T or a class derived from T, the object is
45784598 // initialized from that element (by copy-initialization for
45794599 // copy-list-initialization, or by direct-initialization for
45804600 // direct-list-initialization).
@@ -4585,7 +4605,7 @@ static void TryListInitialization(Sema &S,
45854605 // - Otherwise, if T is an aggregate, [...] (continue below).
45864606 if (S.getLangOpts ().CPlusPlus11 && InitList->getNumInits () == 1 &&
45874607 !IsDesignatedInit) {
4588- if (DestType->isRecordType ()) {
4608+ if (DestType->isRecordType () && DestType-> isAggregateType () ) {
45894609 QualType InitType = InitList->getInit (0 )->getType ();
45904610 if (S.Context .hasSameUnqualifiedType (InitType, DestType) ||
45914611 S.IsDerivedFrom (InitList->getBeginLoc (), InitType, DestType)) {
0 commit comments