===================================================================
@@ -12888,7 +12888,8 @@ cp_parser_function_specifier_opt (cp_parser* parse
/* 14.5.2.3 [temp.mem]
A member function template shall not be virtual. */
- if (PROCESSING_REAL_TEMPLATE_DECL_P ())
+ if (PROCESSING_REAL_TEMPLATE_DECL_P ()
+ && current_class_type)
error_at (token->location, "templates may not be %<virtual%>");
else
set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
===================================================================
@@ -0,0 +1,25 @@
+// PR c++/71099
+
+struct A {
+ virtual void foo();
+};
+
+virtual void A::foo() {} // { dg-error "'virtual' outside class" }
+
+template<typename>
+struct B {
+ virtual void foo();
+};
+
+template<typename T>
+virtual void B<T>::foo() {} // { dg-error "'virtual' outside class" }
+
+template<typename>
+struct C {
+ template<typename>
+ virtual void foo(); // { dg-error "templates may not be 'virtual'" }
+};
+
+template<typename T>
+template<typename>
+virtual void C<T>::foo() {} // { dg-error "'virtual' outside class" }