diff options
author | Marc Chevrier <marc.chevrier@gmail.com> | 2020-01-12 17:26:12 +0100 |
---|---|---|
committer | Marc Chevrier <marc.chevrier@gmail.com> | 2020-01-17 11:09:53 +0100 |
commit | b9a3f2e7e22e3757e863af102c20cc465e1cc02b (patch) | |
tree | c32bb4d30990315954c5dc184b6b7bf5978c2e96 | |
parent | 04c2f67492c5ccc59592e8575a7c66f66aca71a6 (diff) | |
download | cmake-b9a3f2e7e22e3757e863af102c20cc465e1cc02b.tar.gz |
STL Support: enhance behavior of cm::dynamic_reference_cast
Ensure cm::dynamic_reference_cast has same behavior as dynamic_cast
on reference: raise std::bad_cast if dynamic_cast is not possible.
-rw-r--r-- | Utilities/std/cmext/memory | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Utilities/std/cmext/memory b/Utilities/std/cmext/memory index 540a3de2e5..50e79dfe25 100644 --- a/Utilities/std/cmext/memory +++ b/Utilities/std/cmext/memory @@ -6,6 +6,8 @@ #ifndef cmext_memory #define cmext_memory +#include <typeinfo> + #include <cm/type_traits> namespace cm { @@ -24,7 +26,13 @@ template <typename T, typename O, int> = 0> T& dynamic_reference_cast(O& item) { - return *(dynamic_cast<T*>(item.get())); + auto p = dynamic_cast<T*>(item.get()); + + if (p == nullptr) { + throw std::bad_cast(); + } + + return *p; } } // namespace cm |