diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 983b3e19..05b069d9 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -451,6 +451,13 @@ public: } }; + +template +inline constexpr bool is_immutable_v = std::is_integral_v || std::is_floating_point_v + || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v + || std::is_same_v || std::is_same_v + || std::is_pointer_v || std::is_enum_v; + constexpr std::pair _const_cxx_typeid_map[] = { {&typeid(Str), VM::tp_str}, {&typeid(List), VM::tp_list}, @@ -518,8 +525,10 @@ PyObject* py_var(VM* vm, __T&& value){ template __T _py_cast__internal(VM* vm, PyObject* obj) { - using T = std::decay_t<__T>; + static_assert(!std::is_rvalue_reference_v<__T>, "rvalue reference is not allowed"); + using T = std::decay_t<__T>; + if constexpr(std::is_same_v || std::is_same_v){ static_assert(!std::is_reference_v<__T>); // str (shortcuts) @@ -563,7 +572,14 @@ __T _py_cast__internal(VM* vm, PyObject* obj) { }else{ constexpr Type const_type = _find_type_in_const_cxx_typeid_map(); if constexpr(const_type.index >= 0){ - if constexpr(with_check) vm->check_non_tagged_type(obj, const_type); + if constexpr(with_check){ + if constexpr(std::is_same_v){ + // Exception is `subclass_enabled` + vm->check_compatible_type(obj, const_type); + }else{ + vm->check_non_tagged_type(obj, const_type); + } + } return PK_OBJ_GET(T, obj); } }