mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 16:13:53 +00:00
Improved error messaging: Cannot disown nullptr (as_unique_ptr).
This commit is contained in:
parent
4a879cfdcb
commit
6277910abd
@ -134,6 +134,10 @@ struct smart_holder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ensure_use_count_1(const char* context) const {
|
void ensure_use_count_1(const char* context) const {
|
||||||
|
if (vptr.get() == nullptr) {
|
||||||
|
throw std::runtime_error(std::string("Cannot disown nullptr (") +
|
||||||
|
context + ").");
|
||||||
|
}
|
||||||
if (vptr.use_count() != 1) {
|
if (vptr.use_count() != 1) {
|
||||||
throw std::runtime_error(std::string("Cannot disown use_count != 1 (") +
|
throw std::runtime_error(std::string("Cannot disown use_count != 1 (") +
|
||||||
context + ").");
|
context + ").");
|
||||||
|
@ -317,3 +317,10 @@ TEST_CASE("error_disowned_holder", "[E]") {
|
|||||||
hld.as_unique_ptr<int>();
|
hld.as_unique_ptr<int>();
|
||||||
REQUIRE_THROWS_WITH(hld.lvalue_ref<int>(), "Disowned holder (lvalue_ref).");
|
REQUIRE_THROWS_WITH(hld.lvalue_ref<int>(), "Disowned holder (lvalue_ref).");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("error_cannot_disown_nullptr", "[E]") {
|
||||||
|
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
|
||||||
|
hld.as_unique_ptr<int>();
|
||||||
|
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
|
||||||
|
"Cannot disown nullptr (as_unique_ptr).");
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user