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]

Re: C++ PATCH for const_cast


>>>>> "Gabriel" == Gabriel Dos Reis <Gabriel.Dos-Reis@cmla.ens-cachan.fr> writes:

    Gabriel> Mark Mitchell <mark@codesourcery.com> writes:

    Gabriel> | As Gaby pointed out, const_cast was too lenient.
    Gabriel> Here's the fix.

Well, actually, the fix was too strict.  Here's the fixed fix.

    Gabriel> Thanks.

    Gabriel> There is one thing that puzzles me: with you patch is
    Gabriel> this portion of build_const_cast needed anymore?

    Gabriel>   if (same_type_p (TYPE_MAIN_VARIANT (intype),
    Gabriel> TYPE_MAIN_VARIANT (type))) return build_static_cast
    Gabriel> (type, expr);

    Gabriel> If so, why?

I'm not 100% sure, but note the decay_conversion call above there.
That makes it look like:

  void f(int);
  ...
  const_cast<void (*)(int)>(f);

might come through here, for example.

-- 
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com

Index: testsuite/g++.old-deja/g++.pt/cast1.C
===================================================================
RCS file: cast1.C
diff -N cast1.C
--- /dev/null	Sat Dec  5 20:30:03 1998
+++ cast1.C	Fri Apr 30 15:16:15 1999
@@ -0,0 +1,7 @@
+// Build don't link:
+// Origin: Mark Mitchell <mark@codesourcery.com>
+
+template <class T>
+void f (T t) {
+  const_cast<T>(t);
+}
Index: cp/typeck.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/typeck.c,v
retrieving revision 1.155
diff -u -p -r1.155 typeck.c
--- typeck.c	1999/04/29 08:33:46	1.155
+++ typeck.c	1999/04/30 22:16:19
@@ -5643,6 +5643,14 @@ build_const_cast (type, expr)
       return t;
     }
 
+  if (!POINTER_TYPE_P (type) && !TYPE_PTRMEMFUNC_P (type))
+    {
+      cp_error ("`%T' is not a pointer, reference, or pointer-to-member type",
+		type);
+      cp_error ("as required by const_cast");
+      return error_mark_node;
+    }
+
   if (TREE_CODE (type) != REFERENCE_TYPE)
     {
       expr = decay_conversion (expr);


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