diff options
author | Sebastian Redl <sebastian.redl@getdesigned.at> | 2012-01-17 22:50:08 +0000 |
---|---|---|
committer | Sebastian Redl <sebastian.redl@getdesigned.at> | 2012-01-17 22:50:08 +0000 |
commit | 62b7cfb73e202051e7ab0dad42ba213acd0dec7e (patch) | |
tree | 538eaf3463957424716e658e8181dfea311ec2e8 /lib/Sema/SemaTemplateDeduction.cpp | |
parent | 84760e3a5d885ab19b5d11aafe78dfdfe2911e3a (diff) |
Auto deduction support for std::initializer_list, including for-range support. This means you can now write:
for (int i : {1, 4, 512, 23, 251}) {}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148353 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaTemplateDeduction.cpp')
-rw-r--r-- | lib/Sema/SemaTemplateDeduction.cpp | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/lib/Sema/SemaTemplateDeduction.cpp b/lib/Sema/SemaTemplateDeduction.cpp index 738e596ef3..4fff827206 100644 --- a/lib/Sema/SemaTemplateDeduction.cpp +++ b/lib/Sema/SemaTemplateDeduction.cpp @@ -3422,19 +3422,36 @@ Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, return false; TemplateDeductionInfo Info(Context, Loc); - if (DeduceTemplateArgumentsByTypeMatch(*this, &TemplateParams, FuncParam, - InitType, Info, Deduced, TDF)) - return false; + + InitListExpr * InitList = dyn_cast<InitListExpr>(Init); + if (InitList) { + for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { + if (DeduceTemplateArgumentsByTypeMatch(*this, &TemplateParams, FuncParam, + InitList->getInit(i)->getType(), + Info, Deduced, TDF)) + return false; + } + } else { + if (DeduceTemplateArgumentsByTypeMatch(*this, &TemplateParams, FuncParam, + InitType, Info, Deduced, TDF)) + return false; + } QualType DeducedType = Deduced[0].getAsType(); if (DeducedType.isNull()) return false; - + + if (InitList) { + DeducedType = BuildStdInitializerList(DeducedType, Loc); + if (DeducedType.isNull()) + return false; + } + Result = SubstituteAutoTransform(*this, DeducedType).TransformType(Type); - + // Check that the deduced argument type is compatible with the original // argument type per C++ [temp.deduct.call]p4. - if (Result && + if (!InitList && Result && CheckOriginalCallArgDeduction(*this, Sema::OriginalCallArg(FuncParam,0,InitType), Result->getType())) { @@ -3445,6 +3462,17 @@ Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, return true; } +void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { + if (isa<InitListExpr>(Init)) + Diag(VDecl->getLocation(), + diag::err_auto_var_deduction_failure_from_init_list) + << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); + else + Diag(VDecl->getLocation(), diag::err_auto_var_deduction_failure) + << VDecl->getDeclName() << VDecl->getType() << Init->getType() + << Init->getSourceRange(); +} + static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, bool OnlyDeduced, |