@@ -8242,45 +8242,27 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
82428242 return QualType ();
82438243 }
82448244
8245+ // Can't deduce from dependent arguments.
8246+ if (Expr::hasAnyTypeDependentArguments (Inits))
8247+ return Context.DependentTy ;
8248+
82458249 // FIXME: Perform "exact type" matching first, per CWG discussion?
82468250 // Or implement this via an implied 'T(T) -> T' deduction guide?
82478251
82488252 // FIXME: Do we need/want a std::initializer_list<T> special case?
82498253
8254+ // Look up deduction guides, including those synthesized from constructors.
8255+ //
82508256 // C++1z [over.match.class.deduct]p1:
82518257 // A set of functions and function templates is formed comprising:
8252- bool HasDefaultConstructor = false ;
8253- SmallVector<DeclAccessPair, 16 > CtorsAndGuides;
8254- CXXRecordDecl *Primary = Template->getTemplatedDecl ();
8255- bool Complete = isCompleteType (TSInfo->getTypeLoc ().getEndLoc (),
8256- Context.getTypeDeclType (Primary));
8257- if (Complete) {
8258- for (NamedDecl *D : LookupConstructors (Template->getTemplatedDecl ())) {
8259- // - For each constructor of the class template designated by the
8260- // template-name, a function template [...]
8261- auto Info = getConstructorInfo (D);
8262- if (!Info.Constructor || Info.Constructor ->isInvalidDecl ())
8263- continue ;
8264-
8265- // FIXME: Synthesize a deduction guide.
8266-
8267- if (Info.Constructor ->isDefaultConstructor ())
8268- HasDefaultConstructor = true ;
8269- }
8270- }
8271-
8258+ // - For each constructor of the class template designated by the
8259+ // template-name, a function template [...]
82728260 // - For each deduction-guide, a function or function template [...]
82738261 DeclarationNameInfo NameInfo (
82748262 Context.DeclarationNames .getCXXDeductionGuideName (Template),
82758263 TSInfo->getTypeLoc ().getEndLoc ());
82768264 LookupResult Guides (*this , NameInfo, LookupOrdinaryName);
82778265 LookupQualifiedName (Guides, Template->getDeclContext ());
8278- for (auto I = Guides.begin (), E = Guides.end (); I != E; ++I) {
8279- auto *FD = dyn_cast<FunctionDecl>(*I);
8280- if (FD && FD->getMinRequiredArguments () == 0 )
8281- HasDefaultConstructor = true ;
8282- CtorsAndGuides.push_back (I.getPair ());
8283- }
82848266
82858267 // FIXME: Do not diagnose inaccessible deduction guides. The standard isn't
82868268 // clear on this, but they're not found by name so access does not apply.
@@ -8307,8 +8289,8 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
83078289 auto tryToResolveOverload =
83088290 [&](bool OnlyListConstructors) -> OverloadingResult {
83098291 Candidates.clear ();
8310- for (DeclAccessPair Pair : CtorsAndGuides ) {
8311- NamedDecl *D = Pair. getDecl ( )->getUnderlyingDecl ();
8292+ for (auto I = Guides. begin (), E = Guides. end (); I != E; ++I ) {
8293+ NamedDecl *D = (*I )->getUnderlyingDecl ();
83128294 if (D->isInvalidDecl ())
83138295 continue ;
83148296
@@ -8357,10 +8339,11 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
83578339 bool SuppressUserConversions = Kind.isCopyInit ();
83588340
83598341 if (TD)
8360- AddTemplateOverloadCandidate (TD, Pair, /* ExplicitArgs*/ nullptr , Inits,
8361- Candidates, SuppressUserConversions);
8342+ AddTemplateOverloadCandidate (TD, I.getPair (), /* ExplicitArgs*/ nullptr ,
8343+ Inits, Candidates,
8344+ SuppressUserConversions);
83628345 else
8363- AddOverloadCandidate (FD, Pair , Inits, Candidates,
8346+ AddOverloadCandidate (FD, I. getPair () , Inits, Candidates,
83648347 SuppressUserConversions);
83658348 }
83668349 return Candidates.BestViableFunction (*this , Kind.getLocation (), Best);
@@ -8371,7 +8354,21 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
83718354 // C++11 [over.match.list]p1, per DR1467: for list-initialization, first
83728355 // try initializer-list constructors.
83738356 if (ListInit) {
8374- if (ListInit->getNumInits () || !HasDefaultConstructor)
8357+ bool TryListConstructors = true ;
8358+
8359+ // Try list constructors unless the list is empty and the class has one or
8360+ // more default constructors, in which case those constructors win.
8361+ if (!ListInit->getNumInits ()) {
8362+ for (NamedDecl *D : Guides) {
8363+ auto *FD = dyn_cast<FunctionDecl>(D->getUnderlyingDecl ());
8364+ if (FD && FD->getMinRequiredArguments () == 0 ) {
8365+ TryListConstructors = false ;
8366+ break ;
8367+ }
8368+ }
8369+ }
8370+
8371+ if (TryListConstructors)
83758372 Result = tryToResolveOverload (/* OnlyListConstructor*/ true );
83768373 // Then unwrap the initializer list and try again considering all
83778374 // constructors.
@@ -8393,13 +8390,18 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
83938390 Candidates.NoteCandidates (*this , OCD_ViableCandidates, Inits);
83948391 return QualType ();
83958392
8396- case OR_No_Viable_Function:
8393+ case OR_No_Viable_Function: {
8394+ CXXRecordDecl *Primary =
8395+ cast<ClassTemplateDecl>(Template)->getTemplatedDecl ();
8396+ bool Complete =
8397+ isCompleteType (Kind.getLocation (), Context.getTypeDeclType (Primary));
83978398 Diag (Kind.getLocation (),
83988399 Complete ? diag::err_deduced_class_template_ctor_no_viable
83998400 : diag::err_deduced_class_template_incomplete)
8400- << TemplateName << !CtorsAndGuides .empty ();
8401+ << TemplateName << !Guides .empty ();
84018402 Candidates.NoteCandidates (*this , OCD_AllCandidates, Inits);
84028403 return QualType ();
8404+ }
84038405
84048406 case OR_Deleted: {
84058407 Diag (Kind.getLocation (), diag::err_deduced_class_template_deleted)
0 commit comments