@@ -473,13 +473,7 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
473473
474474 static handle cast (T const &src, return_value_policy policy, handle parent) {
475475 // type_caster_base BEGIN
476- // clang-format off
477- if (policy == return_value_policy::automatic ||
478- policy == return_value_policy::automatic_reference ||
479- policy == return_value_policy::reference_override)
480- policy = return_value_policy::copy; // copy ensures constness
481- // clang-format on
482- return cast (&src, policy, parent);
476+ return cast (&src, policy, parent); // copy ensures constness
483477 // type_caster_base END
484478 }
485479
@@ -494,6 +488,13 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
494488 }
495489
496490 static handle cast (T const *src, return_value_policy policy, handle parent) {
491+ // enforce constness by using policy return_value_policy::copy
492+ if (policy != return_value_policy::reference_internal)
493+ policy = return_value_policy::copy;
494+ return cast (const_cast <T *>(src), policy, parent);
495+ }
496+
497+ static handle cast (T *src, return_value_policy policy, handle parent) {
497498 auto st = type_caster_base<T>::src_and_type (src);
498499 return cast_const_raw_ptr ( // Originally type_caster_generic::cast.
499500 st.first ,
@@ -504,10 +505,6 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
504505 make_constructor::make_move_constructor (src));
505506 }
506507
507- static handle cast (T *src, return_value_policy policy, handle parent) {
508- return cast (const_cast <T const *>(src), policy, parent); // Mutbl2Const
509- }
510-
511508#if defined(_MSC_VER) && _MSC_VER < 1910
512509 // Working around MSVC 2015 bug. const-correctness is lost.
513510 // SMART_HOLDER_WIP: IMPROVABLE: make common code work with MSVC 2015.
@@ -567,7 +564,6 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
567564
568565 case return_value_policy::automatic_reference:
569566 case return_value_policy::reference:
570- case return_value_policy::reference_override:
571567 valueptr = src;
572568 wrapper->owned = false ;
573569 break ;
@@ -732,8 +728,8 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
732728 return none ().release ();
733729 if (policy == return_value_policy::automatic)
734730 policy = return_value_policy::reference_internal;
735- else if (policy == return_value_policy::reference_override )
736- policy = return_value_policy::reference;
731+ // allow reference only for overridden method calls (parent is None )
732+ else if ( policy == return_value_policy::reference && !parent) ;
737733 else if (policy != return_value_policy::reference_internal)
738734 throw cast_error (" Invalid return_value_policy for unique_ptr&" );
739735 return smart_holder_type_caster<T>::cast (src.get (), policy, parent);
0 commit comments