===================================================================
@@ -10117,9 +10117,14 @@ grokdeclarator (const cp_declarator *declarator,
if (explicitp == 1 || (explicitp && friendp))
{
- /* [dcl.fct.spec] The explicit specifier shall only be used in
- declarations of constructors within a class definition. */
- error ("only declarations of constructors can be %<explicit%>");
+ /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
+ in the declaration of a constructor or conversion function within
+ a class definition. */
+ if (!current_class_type)
+ error ("%<explicit%> outside class declaration");
+ else
+ error ("only declarations of constructors and conversion operators "
+ "can be %<explicit%>");
explicitp = 0;
}
===================================================================
@@ -0,0 +1,14 @@
+// PR c++/60686
+// { dg-do compile { target c++11 } }
+
+struct A {
+ explicit operator int() const;
+};
+
+explicit inline A::operator int() const { return 1; } // { dg-error "'explicit' outside class declaration" }
+
+struct B {
+ explicit void f(); // { dg-error "only declarations of constructors and conversion operators can be 'explicit'" }
+};
+
+explicit void B::f() { } // { dg-error "'explicit' outside class declaration" }