Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion fast_dynamic_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ namespace fast_dcast
// Pointer to __vftable
using v_table_ptr = const uintptr_t*;

template < bool _B >
struct negation : std::bool_constant<!_B>{};

template < typename _Ty >
__forceinline v_table_ptr get_vtable(const _Ty* ptr)
{
Expand All @@ -81,7 +84,9 @@ namespace fast_dcast
// Should be nothrow but this decreases performance. In practice this is not
// an issue.
template < typename _To, typename _From >
__forceinline _To fast_dynamic_cast(_From* ptr)
__forceinline
std::enable_if_t<negation<std::is_same<clean_type_t<_To>, clean_type_t<_From>>::value || std::is_base_of<clean_type_t<_To>, clean_type_t<_From>>::value>::value, _To>
fast_dynamic_cast(_From* ptr)
{
// Not required, dynamic_cast already produces a compile-time error in this case
// static_assert(std::is_polymorphic<_From>::value, "Type is not polymorphic!");
Expand Down Expand Up @@ -111,6 +116,15 @@ namespace fast_dcast
}
};

// From derived to base.
template < typename _To, typename _From >
__forceinline
std::enable_if_t<std::is_same<clean_type_t<_To>, clean_type_t<_From>>::value || std::is_base_of<clean_type_t<_To>, clean_type_t<_From>>::value, _To>
fast_dynamic_cast(_From* ptr)
{
return ptr;
}

// const T*
template < typename _To, typename _From >
__forceinline const _To fast_dynamic_cast(const _From* ptr)
Expand Down