@@ -474,15 +474,23 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
474474 static handle cast (T const &src, return_value_policy policy, handle parent) {
475475 // type_caster_base BEGIN
476476 // clang-format off
477- if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference)
478- policy = return_value_policy::copy;
479- return cast (&src, policy, parent);
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
480481 // clang-format on
482+ return cast (&src, policy, parent);
481483 // type_caster_base END
482484 }
483485
484486 static handle cast (T &src, return_value_policy policy, handle parent) {
485- return cast (const_cast <T const &>(src), policy, parent); // Mutbl2Const
487+ // type_caster_base BEGIN
488+ // clang-format off
489+ if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference)
490+ policy = return_value_policy::reference;
491+ return cast (&src, policy, parent);
492+ // clang-format on
493+ // type_caster_base END
486494 }
487495
488496 static handle cast (T const *src, return_value_policy policy, handle parent) {
@@ -559,6 +567,7 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
559567
560568 case return_value_policy::automatic_reference:
561569 case return_value_policy::reference:
570+ case return_value_policy::reference_override:
562571 valueptr = src;
563572 wrapper->owned = false ;
564573 break ;
@@ -723,7 +732,9 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
723732 return none ().release ();
724733 if (policy == return_value_policy::automatic)
725734 policy = return_value_policy::reference_internal;
726- if (policy != return_value_policy::reference_internal)
735+ else if (policy == return_value_policy::reference_override)
736+ policy = return_value_policy::reference;
737+ else if (policy != return_value_policy::reference_internal)
727738 throw cast_error (" Invalid return_value_policy for unique_ptr&" );
728739 return smart_holder_type_caster<T>::cast (src.get (), policy, parent);
729740 }
0 commit comments