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

Conversation

clemenssielaff
Copy link
Contributor

Hi there,

I tried and failed to bind a method with an explicit lvalue ref-qualifier (see c++ reference). I understand that we would not want to bind rvalue ref-qualified methods, but the lvalue counterparts should be safe..?

This PR adds two cpp_function constructor overloads for lvalue ref-qualified methods: one for const, the other for not.
I have also added a simple test method with an explicit lvalue ref-qualifier. The code would fail to compile without the additional constructors, now it compiles and works like it should (like a regular, non ref-qualified method).

Let me know what you think and thank you for all your hard work! pybind11 has been a joy to program against and it is used pretty much everywhere I worked.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

This looks good to me, but two thoughts:

  • It's a bit unfortunate that highly complex code is copy-pasted, just to insert another &. But I'm guessing the only way to avoid that is to use a macro, which isn't great either. @wjakob what's your take on this? — If we keep the copy-pasted code, a tiny comment like // Exact copy of above, just with & inserted. may be nice. (It takes a lot of human parsing to figure that out without a comment.)

  • What would it take to also exercise the (const, lvalue ref-qualifier) case? — First idea that comes to mind: introduce a tiny dedicated test class (don't add anything to the existing test class), with mutable int value = 0; // mutable to facilitate testing (const, lvalue ref-qualifier).

@clemenssielaff
Copy link
Contributor Author

Hi Ralf,

thank you for your reply! I have added the suggested comment and moved my test additions out into their own test class and -case.

Agreed that the code duplication is unfortunate, but I do not know of a better way that is shorter and/or more readable. I am open to suggestions though.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

Awesome, thanks!
Just one really tiny nit.

@@ -80,6 +80,8 @@ class cpp_function : public function {
}

/// 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 👍

@wjakob
Copy link
Member

wjakob commented Jun 10, 2020

I wish there was a way to merge all of these constructors using templating but, alas, there is not AFAIK.

@wjakob wjakob merged commit 63df87f into pybind:master Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants