Uh oh!
There was an error while loading. Please reload this page.
[Bugfix] Fix errant const methods - #3194
Conversation
Skylion007
commented
Aug 12, 2021
Seems like there were a few more incorrect ones added in: #3052 |
Skylion007
commented
Aug 12, 2021
Current failures are flakes. |
Skylion007
commented
Aug 13, 2021
rwgk
left a comment
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
jbms
commented
Sep 18, 2021
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: voidfoo(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 |
rwgk
commented
Sep 20, 2021
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: |
jbms
commented
Sep 20, 2021
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*;
voidfoo(dumb_pointer<constint>& x) {
x = nullptr; // allowed
*x = 5; // not allowed
}
voidfoo(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 |
rwgk
commented
Sep 20, 2021
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: