Skip to content

Commit 93d6276

Browse files
committed
fix: add static_assert for polymorphic type restrictions
1 parent 9789d35 commit 93d6276

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src/jspp/binding/TypeConverter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,10 @@ decltype(auto) toCpp(Local<Value> const& value) {
618618
}
619619
// 请求值传递 (T)
620620
else {
621+
static_assert(
622+
!std::is_polymorphic_v<BareT>,
623+
"toCpp<T> with polymorphic T by value is forbidden (slicing UB). Use T&, const T&, or T* instead."
624+
);
621625
if constexpr (!is_conv_ptr && !is_conv_lref) {
622626
if constexpr (std::is_same_v<RawConvRet, BareT>
623627
|| internal::CppValueTypeTransformer_v<RawConvRet, BareT>) {
@@ -1012,6 +1016,11 @@ InstanceGetterCallback wrapInstanceGetter(Fn&& fn, ReturnValuePolicy policy) {
10121016
"First argument of non-member getter must match the bound class. "
10131017
"Expected instance of C&, C*, const C&, or const C*."
10141018
);
1019+
static_assert(
1020+
!(std::is_polymorphic_v<C> && !std::is_lvalue_reference_v<arg_0_type>
1021+
&& !std::is_pointer_v<arg_0_type>),
1022+
"Pass-by-value of a polymorphic type is not allowed (slicing risk). Use C&, const C&, or C* instead."
1023+
);
10151024

10161025
decltype(auto) traitInst = [&] {
10171026
if constexpr (std::is_pointer_v<arg_0_type>) {

0 commit comments

Comments
 (0)