diff mbox

[C++] PR 60686

Message ID 53BAE3D9.10104@oracle.com
State New
Headers show

Commit Message

Paolo Carlini July 7, 2014, 6:15 p.m. UTC
Hi,

in this bug submitter noticed that talking only about explicit 
constructors without mentioning conversion operators is misleading in 
C++11. Thus Jon suggested simply not mentioning the constructors in the 
error message, which I find reasonable because after all it's about the 
fact that the user has 'explicit' on the definition, not about 
'explicit' being used on the wrong type of declaration. Certainly 
anyway, another option would be extending the current error message and 
mention conversion operators too or even providing different error 
messages for C++98 and C++11 (maybe overkill considering that we do 
*accept* explicit operators in C++98 mode, we only emit a pedwarn). 
Tested x86_64-linux.

Thanks,
Paolo.

///////////////////////
/cp
2014-07-07  Jonathan Wakely  <jwakely@redhat.com>
	    Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/60686
	* decl.c (grokdeclarator): Adjust error message about 'explicit'
	outside class definition for C++11.

/testsuite
2014-07-07  Jonathan Wakely  <jwakely@redhat.com>
	    Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/60686
	* g++.dg/cpp0x/explicit8.C: New.

Comments

Jason Merrill July 8, 2014, 10:39 p.m. UTC | #1
On 07/07/2014 11:15 AM, Paolo Carlini wrote:
> +      error ("only declarations can be marked %<explicit%>");

That's pretty unclear, since a definition is a declaration.

Let's split this into three error messages: If the problem is that we're 
outside the class, we should say that.  If the problem is that it's not 
a constructor or conversion function, we should say that.  If the 
problem is that it's not a member of the current class, we should say that.

Jason
diff mbox

Patch

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 212332)
+++ cp/decl.c	(working copy)
@@ -10117,9 +10117,10 @@  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.  */
+      error ("only declarations can be marked %<explicit%>");
       explicitp = 0;
     }
 
Index: testsuite/g++.dg/cpp0x/explicit8.C
===================================================================
--- testsuite/g++.dg/cpp0x/explicit8.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/explicit8.C	(working copy)
@@ -0,0 +1,8 @@ 
+// 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 "only declarations can be marked 'explicit'" }