Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lvalue ref-qualified cpp_function constructors #2213

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 20 additions & 2 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,38 @@ class cpp_function : public function {
(detail::function_signature_t<Func> *) nullptr, extra...);
}

/// Construct a cpp_function from a class method (non-const)
/// Construct a cpp_function from a class method (non-const, no ref-qualifier)
template <typename Return, typename Class, typename... Arg, typename... Extra>
cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) {
initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
(Return (*) (Class *, Arg...)) nullptr, extra...);
}

/// Construct a cpp_function from a class method (const)
/// Construct a cpp_function from a class method (non-const, lvalue ref-qualifier)
/// Is a copy of the overload for non-const functions without explicit ref-qualifier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe delete "Is" here and below?
// A copy of ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

... and done 👍

/// but with an added `&`.
template <typename Return, typename Class, typename... Arg, typename... Extra>
cpp_function(Return (Class::*f)(Arg...)&, const Extra&... extra) {
initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
(Return (*) (Class *, Arg...)) nullptr, extra...);
}

/// Construct a cpp_function from a class method (const, no ref-qualifier)
template <typename Return, typename Class, typename... Arg, typename... Extra>
cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) {
initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
(Return (*)(const Class *, Arg ...)) nullptr, extra...);
}

/// Construct a cpp_function from a class method (const, lvalue ref-qualifier)
/// Is a copy of the overload for const functions without explicit ref-qualifier
/// but with an added `&`.
template <typename Return, typename Class, typename... Arg, typename... Extra>
cpp_function(Return (Class::*f)(Arg...) const&, const Extra&... extra) {
initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
(Return (*)(const Class *, Arg ...)) nullptr, extra...);
}

/// Return the function name
object name() const { return attr("__name__"); }

Expand Down
15 changes: 15 additions & 0 deletions tests/test_methods_and_attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ class RegisteredDerived : public UnregisteredBase {
double sum() const { return rw_value + ro_value; }
};

// Test explicit lvalue ref-qualification
struct RefQualified {
int value = 0;

void refQualified(int other) & { value += other; }
int constRefQualified(int other) const & { return value + other; }
};

TEST_SUBMODULE(methods_and_attributes, m) {
// test_methods_and_attributes
py::class_<ExampleMandA> emna(m, "ExampleMandA");
Expand Down Expand Up @@ -457,4 +465,11 @@ TEST_SUBMODULE(methods_and_attributes, m) {
m.def("custom_caster_destroy_const", []() -> const DestructionTester * { return new DestructionTester(); },
py::return_value_policy::take_ownership); // Likewise (const doesn't inhibit destruction)
m.def("destruction_tester_cstats", &ConstructorStats::get<DestructionTester>, py::return_value_policy::reference);

// test_methods_and_attributes
py::class_<RefQualified>(m, "RefQualified")
.def(py::init<>())
.def_readonly("value", &RefQualified::value)
.def("refQualified", &RefQualified::refQualified)
.def("constRefQualified", &RefQualified::constRefQualified);
}
11 changes: 11 additions & 0 deletions tests/test_methods_and_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,14 @@ def test_custom_caster_destruction():

# Make sure we still only have the original object (from ..._no_destroy()) alive:
assert cstats.alive() == 1


def test_ref_qualified():
"""Tests that explicit lvalue ref-qualified methods can be called just like their
non ref-qualified counterparts."""

r = m.RefQualified()
assert r.value == 0
r.refQualified(17)
assert r.value == 17
assert r.constRefQualified(23) == 40