From ee5d21f63714459f39e0be28fec9dbecf0720505 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 4 Feb 2011 03:57:22 +0000 Subject: [PATCH] When a function template's template parameter has a default argument, it's okay for the following template parameters to not have default arguments (since those template parameters can still be deduced). Also, downgrade the error about default template arguments in function templates to an extension warning, since this is a harmless C++0x extension. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124855 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 6 +++--- lib/Sema/SemaTemplate.cpp | 4 ++-- test/CXX/temp/temp.param/p11-0x.cpp | 13 +++++++++++++ test/CXX/temp/temp.param/p9.cpp | 4 ++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 2c8ccbdfb3..0df95a254f 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1476,9 +1476,9 @@ def note_template_param_prev_default_arg : Note< "previous default template argument defined here">; def err_template_param_default_arg_missing : Error< "template parameter missing a default argument">; -def err_template_parameter_default_in_function_template : Error< - "a template parameter of a function template cannot have a default argument " - "in C++98">; +def ext_template_parameter_default_in_function_template : ExtWarn< + "default template arguments for a function template are a C++0x extension">, + InGroup; def err_template_parameter_default_template_member : Error< "cannot add a default template argument to the definition of a member of a " "class template">; diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 00f490cfad..f7060f03a3 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -1052,7 +1052,7 @@ static bool DiagnoseDefaultTemplateArgument(Sema &S, // (This sentence is not in C++0x, per DR226). if (!S.getLangOptions().CPlusPlus0x) S.Diag(ParamLoc, - diag::err_template_parameter_default_in_function_template) + diag::ext_template_parameter_default_in_function_template) << DefArgRange; return false; @@ -1315,7 +1315,7 @@ bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); Invalid = true; - } else if (MissingDefaultArg) { + } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { // C++ [temp.param]p11: // If a template-parameter of a class template has a default // template-argument, each subsequent template-parameter shall either diff --git a/test/CXX/temp/temp.param/p11-0x.cpp b/test/CXX/temp/temp.param/p11-0x.cpp index d31a1f05fe..0bf4341cf8 100644 --- a/test/CXX/temp/temp.param/p11-0x.cpp +++ b/test/CXX/temp/temp.param/p11-0x.cpp @@ -46,3 +46,16 @@ void f1nt(X1nt); template class... Meta, template class M> void f1tt(X1tt); + +namespace DefaultTemplateArgsInFunction { + template T &f0(U) { T *x = 0; return *x; } + + void test_f0() { + int &ir0 = f0(3.14159); + int &ir1 = f0(3.14159); + float &fr0 = f0(3.14159); + } + + template<> int &f0(int*); + template int &f0(double&); +} diff --git a/test/CXX/temp/temp.param/p9.cpp b/test/CXX/temp/temp.param/p9.cpp index 625477c4e7..62af522cd2 100644 --- a/test/CXX/temp/temp.param/p9.cpp +++ b/test/CXX/temp/temp.param/p9.cpp @@ -2,9 +2,9 @@ // A default template-argument shall not be specified in a function // template declaration or a function template definition -template // expected-error{{cannot have a default argument}} +template // expected-warning{{default template arguments for a function template are a C++0x extension}} void foo0(T); -template // expected-error{{cannot have a default argument}} +template // expected-warning{{default template arguments for a function template are a C++0x extension}} void foo1(T) { } // [...] nor in the template-parameter-list of the definition of a -- 2.40.0