mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-05 21:31:08 -06:00
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.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user