Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/pybind11/detail/smart_holder_poc.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ struct smart_holder {
}
ensure_vptr_is_using_builtin_delete(context);
} else if (!(*rtti_requested == *rtti_uqp_del)) {
throw std::invalid_argument(std::string("Incompatible unique_ptr deleter (") + context
+ ").");
if (!(vptr_is_using_builtin_delete && is_std_default_delete<T>(*rtti_requested))) {
throw std::invalid_argument(std::string("Incompatible unique_ptr deleter (")
+ context + ").");
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/pure_cpp/smart_holder_poc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ struct indestructible_int {
~indestructible_int() = default;
};

struct base {
virtual int get() { return 10; }
virtual ~base() {}
};

struct derived : public base {
int get() override { return 100; }
~derived() override {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we delete this override?
(It seems unusual to override the virtual dtor in this situation.)

};

} // namespace helpers

TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") {
Expand Down Expand Up @@ -227,6 +237,25 @@ TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") {
REQUIRE(*new_owner == 19);
}

TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base", "[E]") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please make this "[S]" here?

[E] is for from/to combinations that generate an error, [S] for valid combinations.

std::unique_ptr<helpers::derived> orig_owner(new helpers::derived());
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
std::unique_ptr<helpers::base> new_owner = hld.as_unique_ptr<helpers::base>();
REQUIRE(!hld.has_pointee());
REQUIRE(new_owner->get() == 100);
}

TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[E]") {
std::unique_ptr<helpers::derived, helpers::functor_other_delete<helpers::derived>> orig_owner(
new helpers::derived());
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
REQUIRE_THROWS_WITH(
(hld.as_unique_ptr<helpers::base, helpers::functor_builtin_delete<helpers::base>>()),
"Incompatible unique_ptr deleter (as_unique_ptr).");
}

TEST_CASE("from_unique_ptr_with_deleter+as_lvalue_ref", "[S]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
Expand Down