Skip to content

Commit a667662

Browse files
FlamefireFlow86
authored andcommitted
Enhance doc for checked_cast and assert type requirements
1 parent cb2bd53 commit a667662

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

libs/common/include/commonDefines.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include "RTTR_Assert.h"
8+
#include <type_traits>
89

910
/// Call a member function trough an object and a member function pointer
1011
#define CALL_MEMBER_FN(object, ptrToMember) ((object).*(ptrToMember))
@@ -24,17 +25,22 @@ inline T absDiff(T a, T b)
2425
return (a > b) ? a - b : b - a;
2526
}
2627

27-
/// Same as static_cast<T> but assert that it actually can be casted via dynamic_cast
28+
/// Same as static_cast<T> but assert the correct dynamic type (in debug mode)
29+
/// Use like: checkedCast<Derived*>(basePtr)
2830
template<typename T, typename T_Src>
2931
inline T checkedCast(T_Src* src)
3032
{
33+
static_assert(std::is_pointer_v<T>, "T must be a pointer type");
3134
RTTR_Assert(!src || dynamic_cast<T>(src));
3235
return static_cast<T>(src);
3336
}
3437

38+
/// Same as static_cast<T&> but assert the correct dynamic type (in debug mode)
39+
/// Use like: checkedCast<Derived>(baseRef)
3540
template<typename T, typename T_Src>
3641
inline T& checkedCast(T_Src& src)
3742
{
43+
static_assert(std::is_class_v<T>, "T must be a plain type");
3844
RTTR_Assert(dynamic_cast<T*>(&src));
3945
return static_cast<T&>(src);
4046
}

0 commit comments

Comments
 (0)