@@ -4200,7 +4200,7 @@ static OverloadingResult ResolveConstructorOverload(
4200
4200
/// \param IsListInit Is this list-initialization?
4201
4201
/// \param IsInitListCopy Is this non-list-initialization resulting from a
4202
4202
/// list-initialization from {x} where x is the same
4203
- /// type as the entity?
4203
+ /// aggregate type as the entity?
4204
4204
static void TryConstructorInitialization(Sema &S,
4205
4205
const InitializedEntity &Entity,
4206
4206
const InitializationKind &Kind,
@@ -4230,6 +4230,14 @@ static void TryConstructorInitialization(Sema &S,
4230
4230
Entity.getKind() !=
4231
4231
InitializedEntity::EK_LambdaToBlockConversionBlockElement);
4232
4232
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
+
4233
4241
// C++17 [dcl.init]p17:
4234
4242
// - If the initializer expression is a prvalue and the cv-unqualified
4235
4243
// version of the source type is the same class as the class of the
@@ -4242,11 +4250,17 @@ static void TryConstructorInitialization(Sema &S,
4242
4250
if (S.getLangOpts().CPlusPlus17 && !RequireActualConstructor &&
4243
4251
UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isPRValue() &&
4244
4252
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
+ }
4250
4264
}
4251
4265
4252
4266
const RecordType *DestRecordType = DestType->getAs<RecordType>();
@@ -4291,6 +4305,12 @@ static void TryConstructorInitialization(Sema &S,
4291
4305
S, Kind.getLocation(), Args, CandidateSet, DestType, Ctors, Best,
4292
4306
CopyInitialization, AllowExplicit,
4293
4307
/*OnlyListConstructors=*/true, IsListInit, RequireActualConstructor);
4308
+
4309
+ if (CopyElisionPossible && Result == OR_No_Viable_Function) {
4310
+ // No initializer list candidate
4311
+ ElideConstructor();
4312
+ return;
4313
+ }
4294
4314
}
4295
4315
4296
4316
// C++11 [over.match.list]p1:
@@ -4572,9 +4592,9 @@ static void TryListInitialization(Sema &S,
4572
4592
return;
4573
4593
}
4574
4594
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
4578
4598
// initialized from that element (by copy-initialization for
4579
4599
// copy-list-initialization, or by direct-initialization for
4580
4600
// direct-list-initialization).
@@ -4585,7 +4605,7 @@ static void TryListInitialization(Sema &S,
4585
4605
// - Otherwise, if T is an aggregate, [...] (continue below).
4586
4606
if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1 &&
4587
4607
!IsDesignatedInit) {
4588
- if (DestType->isRecordType()) {
4608
+ if (DestType->isRecordType() && DestType->isAggregateType() ) {
4589
4609
QualType InitType = InitList->getInit(0)->getType();
4590
4610
if (S.Context.hasSameUnqualifiedType(InitType, DestType) ||
4591
4611
S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) {
0 commit comments