public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Only allow downcasts in checked_static_cast
@ 2023-02-11 16:17 Tom Tromey
  0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2023-02-11 16:17 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

It didn't make sense to me that checked_static_cast allows upcasts, as
it seems to me that it is better to simply not write an explicit
upcast, but instead just allow the implicit derived-to-base
conversion.

This patch removes the downcast check from checked_static_cast.  It
also doesn't allow casts to the identical type, as this also did not
seem necessary.

Tested by rebuilding.
---
 gdbsupport/gdb-checked-static-cast.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/gdbsupport/gdb-checked-static-cast.h b/gdbsupport/gdb-checked-static-cast.h
index bc75244bddd..ede0f6da1bc 100644
--- a/gdbsupport/gdb-checked-static-cast.h
+++ b/gdbsupport/gdb-checked-static-cast.h
@@ -30,7 +30,10 @@ namespace gdb
    work for polymorphic types.
 
    In non-developer (i.e. production) builds, the dynamic_cast is replaced
-   with a static_cast which is usually significantly faster.  */
+   with a static_cast which is usually significantly faster.
+
+   This is intended only to be used for downcasts.  For upcasts, you
+   can safely omit the cast.  */
 
 template<typename T, typename V>
 T
@@ -48,10 +51,10 @@ checked_static_cast (V *v)
   /* In developer mode this cast uses dynamic_cast to confirm at run-time
      that the cast from V* to T is valid.  However, we can catch some
      mistakes at compile time, this assert prevents anything other than
-     downcasts, or casts to same type.  */
+     downcasts.  */
   static_assert (std::is_base_of<V, T_no_P>::value
-		 || std::is_base_of<T_no_P, V>::value,
-		 "types must be related");
+		 && !std::is_same<V, T_no_P>::value,
+		 "target type must be derived from actual type");
 
 #ifdef DEVELOPMENT
   if (v == nullptr)
-- 
2.39.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-02-11 16:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-11 16:17 [PATCH] Only allow downcasts in checked_static_cast Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).