@@ -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,14 @@ 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+ // The only exception is reference_internal, which can be used to ignore constness
493+ if (policy != return_value_policy::reference_internal)
494+ policy = return_value_policy::copy;
495+ return cast (const_cast <T *>(src), policy, parent);
496+ }
497+
498+ static handle cast (T *src, return_value_policy policy, handle parent) {
497499 auto st = type_caster_base<T>::src_and_type (src);
498500 return cast_const_raw_ptr ( // Originally type_caster_generic::cast.
499501 st.first ,
@@ -504,10 +506,6 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
504506 make_constructor::make_move_constructor (src));
505507 }
506508
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-
511509#if defined(_MSC_VER) && _MSC_VER < 1910
512510 // Working around MSVC 2015 bug. const-correctness is lost.
513511 // SMART_HOLDER_WIP: IMPROVABLE: make common code work with MSVC 2015.
@@ -567,7 +565,6 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
567565
568566 case return_value_policy::automatic_reference:
569567 case return_value_policy::reference:
570- case return_value_policy::reference_override:
571568 valueptr = src;
572569 wrapper->owned = false ;
573570 break ;
@@ -732,8 +729,8 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
732729 return none ().release ();
733730 if (policy == return_value_policy::automatic)
734731 policy = return_value_policy::reference_internal;
735- else if (policy == return_value_policy::reference_override )
736- policy = return_value_policy::reference;
732+ // allow reference only for overridden method calls (parent is None )
733+ else if ( policy == return_value_policy::reference && !parent) ;
737734 else if (policy != return_value_policy::reference_internal)
738735 throw cast_error (" Invalid return_value_policy for unique_ptr&" );
739736 return smart_holder_type_caster<T>::cast (src.get (), policy, parent);
0 commit comments