This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PR c++/17788] add decl for implicit default ctor even if ill-formed


The C++ Standard says a default ctor is implicitly declared for any
class that doesn't have ctors explicitly declared, and that such ctor
is defined only if it is actually used, so, if it is ill-formed but
never used, no problem occurs.

Unfortunately, the current implementation refrains from adding a
declaration for the constructor if it finds that it would be
ill-formed.  As a result, the diagnostic we produce alludes to failed
overload resolution, giving the implicitly-declared copy ctor as the
only option, although with the wrong number of arguments.  This is
misleading.

I think we'd be better off not trying to decide upfront whether the
ctor would be ill-formed or not and, should it be needed but
ill-formed, emit a good error message for it.  This is what this small
patch accomplishes.

Tested in x86_64-linux-gnu.  Ok to install?

Index: gcc/cp/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	PR c++/17788
	* class.c (add_implicitly_declared_members): Ignore
	cant_have_default_ctor.

Index: gcc/cp/class.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/class.c,v
retrieving revision 1.703
diff -u -p -r1.703 class.c
--- gcc/cp/class.c 9 Feb 2005 02:53:35 -0000 1.703
+++ gcc/cp/class.c 9 Feb 2005 17:38:10 -0000
@@ -2464,7 +2464,7 @@ maybe_add_class_template_decl_list (tree
 
 static void
 add_implicitly_declared_members (tree t, 
-                                 int cant_have_default_ctor,
+                                 int cant_have_default_ctor ATTRIBUTE_UNUSED,
 				 int cant_have_const_cctor,
 				 int cant_have_const_assignment)
 {
@@ -2517,10 +2517,21 @@ add_implicitly_declared_members (tree t,
     }
 
   /* Default constructor.  */
-  if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
+  if (! TYPE_HAS_CONSTRUCTOR (t))
     {
       TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
       CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
+      /* We do not take cant_have_default_ctor into account here.  The
+	 tests we perform elsewhere to set it are either redundant
+	 with the test above, or incorrect in that they tell whether
+	 the default constructor, if implicitly defined, would be
+	 well-formed or not, as opposed to whether a declaration of
+	 such an implicit default constructor should be available.
+	 Although we do end up generating a diagnostic in both cases,
+	 if appropriate, `overload resolution failed for
+	 foo::foo(void)' is far more confusing and less informative
+	 than `initializer for member foo::bar missing in constructor
+	 foo::foo(void)'.  */
     }
 
   /* Copy constructor.  */
Index: gcc/testsuite/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	PR c++/17788
	* g++.dg/init/ctor4.C: New.

Index: gcc/testsuite/g++.dg/init/ctor4.C
===================================================================
RCS file: gcc/testsuite/g++.dg/init/ctor4.C
diff -N gcc/testsuite/g++.dg/init/ctor4.C
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/g++.dg/init/ctor4.C 9 Feb 2005 17:38:25 -0000
@@ -0,0 +1,20 @@
+// PR c++/17788
+// { dg-do compile }
+
+class foo {
+public:
+  foo();
+};
+
+class bar: public foo {
+private:
+  int &a;
+};
+
+foo::foo() {
+}
+
+int main(int argc, char **argv)
+{
+  bar x; // { dg-error "uninitialized" }
+}
-- 
Alexandre Oliva             http://www.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist  oliva@{lsd.ic.unicamp.br, gnu.org}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]