[C++ Patch] PR 56380

Paolo Carlini paolo.carlini@oracle.com
Thu Aug 22 12:12:00 GMT 2013


Hi,

we fail to diagnose such illegal declarations when they occur at 
instantiation time.

Patch should be rather straightforward, possibly modulo exact wording; 
permerror vs error; something error recovery related (not touching the 
CLASSTYPE_HAS_MUTABLE setting seems ok to me).

Booted and tested x86_64-linux.

Thanks,
Paolo.

//////////////////////////
-------------- next part --------------
/cp
2013-08-22  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/56380
	* class.c (check_field_decls): Check for const mutable and const
	reference data members.

/testsuite
2013-08-22  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/56380
	* g++.dg/template/error54.C: New.
-------------- next part --------------
Index: cp/class.c
===================================================================
--- cp/class.c	(revision 201917)
+++ cp/class.c	(working copy)
@@ -3500,6 +3500,22 @@ check_field_decls (tree t, tree *access_decls,
       if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
 	CLASSTYPE_HAS_MUTABLE (t) = 1;
 
+      if (DECL_MUTABLE_P (x))
+	{
+	  if (CP_TYPE_CONST_P (type))
+	    {
+	      error ("member %q+D cannot be declared both %<const%> "
+		     "and %<mutable%>", x);
+	      continue;
+	    }
+	  if (TREE_CODE (type) == REFERENCE_TYPE)
+	    {
+	      error ("member %q+D cannot be declared as a %<mutable%> "
+		     "reference", x);
+	      continue;
+	    }
+	}
+
       if (! layout_pod_type_p (type))
 	/* DR 148 now allows pointers to members (which are POD themselves),
 	   to be allowed in POD structs.  */
Index: testsuite/g++.dg/template/error54.C
===================================================================
--- testsuite/g++.dg/template/error54.C	(revision 0)
+++ testsuite/g++.dg/template/error54.C	(working copy)
@@ -0,0 +1,10 @@
+// PR c++/56380
+
+template <typename T>
+struct X {
+  X();
+  mutable T x;  // { dg-error "cannot be declared" }
+};
+
+X<const int> a; // { dg-message "required from here" }
+X<int&> b;      // { dg-message "required from here" }


More information about the Gcc-patches mailing list