From: Richard Biener Date: Fri, 21 Apr 2023 11:38:53 +0000 (+0200) Subject: Add safe_is_a X-Git-Tag: basepoints/gcc-15~9995 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2e047c00e983b6d9037906feac3a215ef829c82f;p=thirdparty%2Fgcc.git 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 --git a/gcc/is-a.h b/gcc/is-a.h index b5355242655a..0a697cf002a2 100644 --- a/gcc/is-a.h +++ b/gcc/is-a.h @@ -232,6 +232,19 @@ is_a (U *p) return is_a_helper::test (p); } +/* Similar to is_a<>, but where the pointer can be NULL, even if + is_a_helper doesn't check for NULL. */ + +template +inline bool +safe_is_a (U *p) +{ + if (p) + return is_a_helper ::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. */