{"action":"created","issue":{"url":"https://api.github.com/repos/pybind/pybind11/issues/254","repository_url":"https://api.github.com/repos/pybind/pybind11","labels_url":"https://api.github.com/repos/pybind/pybind11/issues/254/labels{/name}","comments_url":"https://api.github.com/repos/pybind/pybind11/issues/254/comments","events_url":"https://api.github.com/repos/pybind/pybind11/issues/254/events","html_url":"https://github.com/pybind/pybind11/issues/254","id":162706192,"number":254,"title":"Class method returns object itself","user":{"login":"nafur","id":1126854,"avatar_url":"https://avatars.githubusercontent.com/u/1126854?v=3","gravatar_id":"","url":"https://api.github.com/users/nafur","html_url":"https://github.com/nafur","followers_url":"https://api.github.com/users/nafur/followers","following_url":"https://api.github.com/users/nafur/following{/other_user}","gists_url":"https://api.github.com/users/nafur/gists{/gist_id}","starred_url":"https://api.github.com/users/nafur/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nafur/subscriptions","organizations_url":"https://api.github.com/users/nafur/orgs","repos_url":"https://api.github.com/users/nafur/repos","events_url":"https://api.github.com/users/nafur/events{/privacy}","received_events_url":"https://api.github.com/users/nafur/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-06-28T14:53:18Z","updated_at":"2016-06-28T15:27:15Z","closed_at":"2016-06-28T15:17:28Z","body":"I have roughly the following code:\r\n```cpp\r\nclass N {};\r\nclass Interval {\r\n  N mLower;\r\n  N mUpper;\r\npublic:\r\n  inline const N& lower() { return mLower; }\r\n  inline const N& upper() { return mUpper; }\r\n};\r\n```\r\n```cpp\r\npy::class_<Interval>(m, \"Interval\")\r\n  .def(\"lower\", &Interval::lower)\r\n  .def(\"upper\", &Interval::upper);\r\n```\r\nOf course, `N` is also known to python and `Interval` has a bunch of other methods.\r\n\r\nNow the following happens:\r\n```python\r\n> i = Interval(1,2)\r\n> print(i)\r\n[1,2]\r\n> i.lower()\r\n[1,2]\r\n> i.upper()\r\n2\r\n```\r\n`lower()` seems to return the object itself, not the member it is supposed to. `upper()` however works fine, though everything is implemented analogously.\r\n\r\nWe have unit tests in place for the C++ side, `Interval::lower()` works fine.\r\n\r\nI managed to find a workaround here: using `.def(\"lower\", &Interval::lower, py::return_value_policy::reference_internal)` actually fixes the problem.\r\nStill I'd really like to know what's going on here...\r\nIn my understanding, pybind should use `return_value::copy` by default, which should also be fine. (Though it is probably a bit slower...)"},"comment":{"url":"https://api.github.com/repos/pybind/pybind11/issues/comments/229085008","html_url":"https://github.com/pybind/pybind11/issues/254#issuecomment-229085008","issue_url":"https://api.github.com/repos/pybind/pybind11/issues/254","id":229085008,"user":{"login":"nafur","id":1126854,"avatar_url":"https://avatars.githubusercontent.com/u/1126854?v=3","gravatar_id":"","url":"https://api.github.com/users/nafur","html_url":"https://github.com/nafur","followers_url":"https://api.github.com/users/nafur/followers","following_url":"https://api.github.com/users/nafur/following{/other_user}","gists_url":"https://api.github.com/users/nafur/gists{/gist_id}","starred_url":"https://api.github.com/users/nafur/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nafur/subscriptions","organizations_url":"https://api.github.com/users/nafur/orgs","repos_url":"https://api.github.com/users/nafur/repos","events_url":"https://api.github.com/users/nafur/events{/privacy}","received_events_url":"https://api.github.com/users/nafur/received_events","type":"User","site_admin":false},"created_at":"2016-06-28T15:27:14Z","updated_at":"2016-06-28T15:27:14Z","body":"Okay, thanks!\r\nDid I simply miss this in [the docs](https://pybind11.readthedocs.io/en/latest/advanced.html#return-value-policies) or do the docs fail to mentioned it (yet)?\r\nI'd expect that either references are handled correctly out of the box, or that the docs state that you should use the correct policy, otherwise you may get undefined behaviour.\r\n\r\nIn this case, according to the docs, `automatic` should be suboptimal (as it induces a copy) but not incorrect..."}}
{"actor":{"display_login":"nafur"}}