Skip to content

Commit cc36ac5

Browse files
oremanjpre-commit-ci[bot]rwgk
authored
type_caster_generic: fix compiler error when casting a T that is implicitly convertible from T* (#5873)
* type_caster_generic: fix compiler error when casting a T that is implicitly convertible from T* * style: pre-commit fixes * Placate clang-tidy * Expand NOLINT to specify Clang-Tidy check names --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ralf W. Grosse-Kunstleve <[email protected]>
1 parent a2c5971 commit cc36ac5

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

include/pybind11/detail/type_caster_base.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ struct cast_sources {
563563
// Use the given pointer with its compile-time type, possibly downcast
564564
// via polymorphic_type_hook()
565565
template <typename itype>
566-
cast_sources(const itype *ptr); // NOLINT(google-explicit-constructor)
566+
explicit cast_sources(const itype *ptr);
567567

568568
// Use the given pointer and type
569569
// NOLINTNEXTLINE(google-explicit-constructor)
@@ -1621,8 +1621,7 @@ class type_caster_base : public type_caster_generic {
16211621
// that's correct in this context, so you can't use type_caster_base<A>
16221622
// to convert an unrelated B* to Python.
16231623
struct cast_sources : detail::cast_sources {
1624-
// NOLINTNEXTLINE(google-explicit-constructor)
1625-
cast_sources(const itype *ptr) : detail::cast_sources(ptr) {}
1624+
explicit cast_sources(const itype *ptr) : detail::cast_sources(ptr) {}
16261625
};
16271626

16281627
static handle cast(const itype &src, return_value_policy policy, handle parent) {
@@ -1637,6 +1636,10 @@ class type_caster_base : public type_caster_generic {
16371636
return cast(std::addressof(src), return_value_policy::move, parent);
16381637
}
16391638

1639+
static handle cast(const itype *src, return_value_policy policy, handle parent) {
1640+
return cast(cast_sources{src}, policy, parent);
1641+
}
1642+
16401643
static handle cast(const cast_sources &srcs, return_value_policy policy, handle parent) {
16411644
return type_caster_generic::cast(srcs,
16421645
policy,
@@ -1645,6 +1648,10 @@ class type_caster_base : public type_caster_generic {
16451648
make_move_constructor((const itype *) nullptr));
16461649
}
16471650

1651+
static handle cast_holder(const itype *src, const void *holder) {
1652+
return cast_holder(cast_sources{src}, holder);
1653+
}
1654+
16481655
static handle cast_holder(const cast_sources &srcs, const void *holder) {
16491656
auto policy = return_value_policy::take_ownership;
16501657
return type_caster_generic::cast(srcs, policy, {}, nullptr, nullptr, holder);

tests/test_class.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class ForwardClass;
5858
class Args : public py::args {};
5959
} // namespace pr5396_forward_declared_class
6060

61+
struct ConvertibleFromAnything {
62+
ConvertibleFromAnything() = default;
63+
template <class T>
64+
// NOLINTNEXTLINE(bugprone-forwarding-reference-overload,google-explicit-constructor)
65+
ConvertibleFromAnything(T &&) {}
66+
};
67+
6168
} // namespace test_class
6269

6370
static_assert(py::detail::is_same_or_base_of<py::args, py::args>::value, "");
@@ -578,6 +585,11 @@ TEST_SUBMODULE(class_, m) {
578585
});
579586

580587
test_class::pr4220_tripped_over_this::bind_empty0(m);
588+
589+
// Regression test for compiler error that showed up in #5866
590+
m.def("return_universal_recipient", []() -> test_class::ConvertibleFromAnything {
591+
return test_class::ConvertibleFromAnything{};
592+
});
581593
}
582594

583595
template <int N>

0 commit comments

Comments
 (0)