public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] Only allow downcasts in checked_static_cast
Date: Sat, 11 Feb 2023 09:17:51 -0700	[thread overview]
Message-ID: <20230211161751.3813270-1-tom@tromey.com> (raw)

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


                 reply	other threads:[~2023-02-11 16:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230211161751.3813270-1-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).