From: Anders Carlsson Date: Sat, 22 Jan 2011 14:43:56 +0000 (+0000) Subject: A member function template cannot be virtual. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f1602a56f89ac48aa1b5bce42f07ee795e674a6f;p=clang A member function template cannot be virtual. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124031 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index e4b4685552..0743d16124 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -642,6 +642,8 @@ def err_virtual_non_function : Error< "'virtual' can only appear on non-static member functions">; def err_virtual_out_of_class : Error< "'virtual' can only be specified inside the class definition">; +def err_virtual_member_function_template : Error< + "'virtual' can not be specified on member function templates">; def err_static_overrides_virtual : Error< "'static' member function %0 overrides a virtual function in a base class">; def err_explicit_non_function : Error< diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index be6d60e657..a266b60ac3 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -3705,7 +3705,14 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC, diag::err_virtual_non_function); } else if (!CurContext->isRecord()) { // 'virtual' was specified outside of the class. - Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_virtual_out_of_class) + Diag(D.getDeclSpec().getVirtualSpecLoc(), + diag::err_virtual_out_of_class) + << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); + } else if (NewFD->getDescribedFunctionTemplate()) { + // C++ [temp.mem]p3: + // A member function template shall not be virtual. + Diag(D.getDeclSpec().getVirtualSpecLoc(), + diag::err_virtual_member_function_template) << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); } else { // Okay: Add virtual to the method. diff --git a/test/CXX/temp/temp.decls/temp.mem/p3.cpp b/test/CXX/temp/temp.decls/temp.mem/p3.cpp new file mode 100644 index 0000000000..0eb747be20 --- /dev/null +++ b/test/CXX/temp/temp.decls/temp.mem/p3.cpp @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +template struct AA { + template virtual void g(C); // expected-error{{'virtual' can not be specified on member function templates}} + virtual void f(); +};