@@ -685,12 +685,10 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
685
685
smart_holder_type_caster_class_hooks {
686
686
static constexpr auto name = _<std::unique_ptr<T, D>>();
687
687
688
- static handle cast (std::unique_ptr<T, D> &&src, return_value_policy policy, handle parent) {
689
- if (policy != return_value_policy::automatic
690
- && policy != return_value_policy::reference_internal
691
- && policy != return_value_policy::move) {
688
+ static handle cast (std::unique_ptr<T, D> &&src, return_value_policy policy, handle) {
689
+ if (policy != return_value_policy::automatic && policy != return_value_policy::move) {
692
690
// SMART_HOLDER_WIP: IMPROVABLE: Error message.
693
- throw cast_error (" Invalid return_value_policy for unique_ptr. " );
691
+ throw cast_error (" Invalid return_value_policy: unique_ptr&& can only move " );
694
692
}
695
693
696
694
auto src_raw_ptr = src.get ();
@@ -712,19 +710,23 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
712
710
auto smhldr = pybindit::memory::smart_holder::from_unique_ptr (std::move (src));
713
711
tinfo->init_instance (inst_raw_ptr, static_cast <const void *>(&smhldr));
714
712
715
- if (policy == return_value_policy::reference_internal)
716
- keep_alive_impl (inst, parent);
717
-
718
713
return inst.release ();
719
714
}
715
+ static handle cast (std::unique_ptr<T, D> &, return_value_policy, handle) {
716
+ throw cast_error (" Passing non-const unique_ptr& is not supported. "
717
+ " If you want to transfer ownership, use unique_ptr&&. "
718
+ " If you want to return a reference, use unique_ptr const&." );
719
+ }
720
+
720
721
static handle
721
722
cast (const std::unique_ptr<T, D> &src, return_value_policy policy, handle parent) {
722
723
if (!src)
723
724
return none ().release ();
724
725
if (policy == return_value_policy::automatic)
725
726
policy = return_value_policy::reference_internal;
726
727
if (policy != return_value_policy::reference_internal)
727
- throw cast_error (" Invalid return_value_policy for unique_ptr&" );
728
+ throw cast_error (
729
+ " Invalid return_value_policy: unique_ptr const& expects reference_internal" );
728
730
return smart_holder_type_caster<T>::cast (src.get (), policy, parent);
729
731
}
730
732
0 commit comments