public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-144] Add safe_is_a
@ 2023-04-21 13:53 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2023-04-21 13:53 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:2e047c00e983b6d9037906feac3a215ef829c82f

commit r14-144-g2e047c00e983b6d9037906feac3a215ef829c82f
Author: Richard Biener <rguenther@suse.de>
Date:   Fri Apr 21 13:38:53 2023 +0200

    Add safe_is_a
    
    The following adds safe_is_a, an is_a check handling nullptr
    gracefully.
    
            * is-a.h (safe_is_a): New.

Diff:
---
 gcc/is-a.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/gcc/is-a.h b/gcc/is-a.h
index b5355242655..0a697cf002a 100644
--- a/gcc/is-a.h
+++ b/gcc/is-a.h
@@ -232,6 +232,19 @@ is_a (U *p)
   return is_a_helper<T>::test (p);
 }
 
+/* Similar to is_a<>, but where the pointer can be NULL, even if
+   is_a_helper<T> doesn't check for NULL.  */
+
+template <typename T, typename U>
+inline bool
+safe_is_a (U *p)
+{
+  if (p)
+    return is_a_helper <T>::test (p);
+  else
+    return false;
+}
+
 /* A generic conversion from a base type U to a derived type T.  See the
    discussion above for when to use this function.  */

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

only message in thread, other threads:[~2023-04-21 13:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-21 13:53 [gcc r14-144] Add safe_is_a Richard Biener

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).