[RFC] is-a.h supporting casting from non const to const

Andrew Pinski andrew.pinski@caviumnetworks.com
Sun Nov 23 20:52:00 GMT 2014


Hi,
  While working on a patch to change some error to inform, I changed
the as_a cast in RTL_LOCATION to be as_a <const rtx_insn *>.  This
falls down when you start with a rtx rather than a const_rtx..

I had to modify is-a.h to this but I really don't like the
modification at all since it shows the bigger issues with templates.
Can anyone think of a better way of supporting this?  Attached is my
patch currently which is able to bootstrap with the modification to
RTL_LOCATION being there.

Thanks,
Andrew Pinski
-------------- next part --------------
Index: is-a.h
===================================================================
--- is-a.h	(revision 217985)
+++ is-a.h	(working copy)
@@ -147,10 +147,26 @@ struct is_a_helper
 {
   template <typename U>
   static inline bool test (U *p);
+  static inline bool test (T *) { return true; }
   template <typename U>
   static inline T cast (U *p);
 };
 
+
+template <typename T>
+struct is_a_helper<const T*>
+{
+  template <typename U>
+  static inline bool test (const U *p);
+  template <typename U>
+  static inline bool test (U *p) {return test(const_cast<const U*>(p));}
+  static inline bool test (const T *) { return true; }
+  template <typename U>
+  static inline const T *cast (const U *p);
+  template <typename U>
+  static inline const T *cast (U *p) { return cast(const_cast<const U*>(p)); }
+};
+
 /* Note that we deliberately do not define the 'test' member template.  Not
    doing so will result in a build-time error for type relationships that have
    not been defined, rather than a run-time error.  See the discussion above
@@ -169,6 +185,15 @@ is_a_helper <T>::cast (U *p)
 }
 
 
+template <typename T>
+template <typename U>
+inline const T*
+is_a_helper <const T*>::cast (const U *p)
+{
+  return reinterpret_cast <const T*> (p);
+}
+
+
 /* The public interface.  */
 
 /* A generic test for a type relationship.  See the discussion above for when


More information about the Gcc mailing list