@@ -677,12 +677,10 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
677
677
smart_holder_type_caster_class_hooks {
678
678
static constexpr auto name = _<std::unique_ptr<T, D>>();
679
679
680
- static handle cast (std::unique_ptr<T, D> &&src, return_value_policy policy, handle parent) {
681
- if (policy != return_value_policy::automatic
682
- && policy != return_value_policy::reference_internal
683
- && policy != return_value_policy::move) {
680
+ static handle cast (std::unique_ptr<T, D> &&src, return_value_policy policy, handle) {
681
+ if (policy != return_value_policy::automatic && policy != return_value_policy::move) {
684
682
// SMART_HOLDER_WIP: IMPROVABLE: Error message.
685
- throw cast_error (" Invalid return_value_policy for unique_ptr. " );
683
+ throw cast_error (" Invalid return_value_policy: unique_ptr&& can only move " );
686
684
}
687
685
688
686
auto src_raw_ptr = src.get ();
@@ -704,19 +702,23 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
704
702
auto smhldr = pybindit::memory::smart_holder::from_unique_ptr (std::move (src));
705
703
tinfo->init_instance (inst_raw_ptr, static_cast <const void *>(&smhldr));
706
704
707
- if (policy == return_value_policy::reference_internal)
708
- keep_alive_impl (inst, parent);
709
-
710
705
return inst.release ();
711
706
}
707
+ static handle cast (std::unique_ptr<T, D> &, return_value_policy, handle) {
708
+ throw cast_error (" Passing non-const unique_ptr& is not supported.\n "
709
+ " If you want to transfer ownership, use unique_ptr&&.\n "
710
+ " If you want to return a reference, use unique_ptr const&." );
711
+ }
712
+
712
713
static handle
713
714
cast (const std::unique_ptr<T, D> &src, return_value_policy policy, handle parent) {
714
715
if (!src)
715
716
return none ().release ();
716
717
if (policy == return_value_policy::automatic)
717
718
policy = return_value_policy::reference_internal;
718
719
if (policy != return_value_policy::reference_internal)
719
- throw cast_error (" Invalid return_value_policy for unique_ptr&" );
720
+ throw cast_error (
721
+ " Invalid return_value_policy: unique_ptr const& expects reference_internal" );
720
722
return smart_holder_type_caster<T>::cast (src.get (), policy, parent);
721
723
}
722
724
0 commit comments