-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Bugfix] Fix errant const methods #3194
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
[Bugfix] Fix errant const methods #3194
Conversation
Seems like there were a few more incorrect ones added in: #3052 |
Current failures are flakes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say: merge! :-)
If we notice other py-non-const
methods later we can take care of them as we discover them. Better than spending time looking for needles in the haystack.
The collection methods like The reason is that Note that I'm referring specifically to the semantics of the type, not the implementation. A type like The only non-const methods of a type with pointer semantics should be ones that actually modify the pointer itself, not the pointee. That is the only approach that is consistent. The way it is currently, you can trivially "defeat" the const by making a copy: void foo(const pybind11::dict &x) {
pybind11::dict copy = x;
copy.clear();
} Essentially, Since Python itself always uses pointer semantics, and doesn't have const objects other than the specialized frozen collections, I don't think it is particularly helpful to try to add an equivalent of |
We had a little bit of a discussion in the pybind11 slack channel before this PR was merged. I'm copying a selected comment from back then below. I don't think there is a way to make this "right". I agree with @jbms' observations, but feel it's best to not kick up more dust over this. Ralf Grosse-Kunstleve Aug 12, 2021, 12:46 PM Of course ... this happily compiles ... I keep forgetting how annoying the C++ const semantics are:
|
It is true that the lack of built-in support for deep const in C++ leaves something to be desired. But I would still suggest being consistent with how const propagation works in C++. For pointer-semantic classes templated on a type parameter, it usually works well to use const-qualification of the type parameter to indicate inner-const, consistent with built-in pointer types and template <typename T>
using dumb_pointer = T*;
void foo(dumb_pointer<const int>& x) {
x = nullptr; // allowed
*x = 5; // not allowed
}
void foo(const dumb_pointer<int>& x) {
x = nullptr; // not allowed
*x = 5; // allowed
} When dealing with non-templated pointer-semantic types, it is unfortunately more annoying to also define an inner-const version. You could either use a separate name, like In any case, for pybind11 I would argue that inner-const wrappers of Python types are not really needed since Python itself does not really support const except via separate types like |
Yeah, adding I'm fine either way, with what we have, or adding the |
I'm not very invested in this at the moment, but from general principles, I'm mildly in favor of following C++ here. We should follow the language most of the time (the only exception is the const return for Eigen, but there we are intentionally using a language construct for a feature). |
Description
Suggested changelog entry: