From 562eab817cafc51cd857c9bf384fd0b74ea7aa11 Mon Sep 17 00:00:00 2001 From: Steve Vinoski Date: Thu, 15 Nov 2018 11:01:51 -0500 Subject: [PATCH] Do not throw std::bad_cast from antlrcpp::Any::is The antlrcpp::Any::is function should not throw a std::bad_cast exception if the contained type can't be cast to the requested type, but should instead just return a boolean result. Add a boolean parameter to the private getDerived helper function to allow callers to specify whether or not they want the cast results checked. In the is() function, pass false for this parameter; in the as() functions, pass true. --- runtime/Cpp/runtime/src/support/Any.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/runtime/Cpp/runtime/src/support/Any.h b/runtime/Cpp/runtime/src/support/Any.h index 50fc1ed9f..5db59f6e6 100644 --- a/runtime/Cpp/runtime/src/support/Any.h +++ b/runtime/Cpp/runtime/src/support/Any.h @@ -46,21 +46,21 @@ struct ANTLR4CPP_PUBLIC Any template bool is() const { - auto derived = getDerived(); + auto derived = getDerived(false); return derived != nullptr; } template StorageType& as() { - auto derived = getDerived(); + auto derived = getDerived(true); return derived->value; } template const StorageType& as() const { - auto derived = getDerived(); + auto derived = getDerived(true); return derived->value; } @@ -143,12 +143,12 @@ private: } template - Derived>* getDerived() const { + Derived>* getDerived(bool checkCast) const { typedef StorageType T; auto derived = dynamic_cast*>(_ptr); - if (!derived) + if (checkCast && !derived) throw std::bad_cast(); return derived;