[BUG]: Pass by reference does copy
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
2.10.2
Problem description
The docs state that passing arguments as reference and pointer should work, but actually, while pointers works well, references dont. They end up beeing copied, which is a blocker if the type is not copy constructible.
class PyPlugin
{
public:
virtual void takeThisAndModifyR(StandardItem &item) {};
virtual void takeThisAndModifyP(StandardItem *item) {};
};
class PyPluginTrampoline : PyPlugin
{
public:
using PyPlugin::PyPlugin; // Inherit the constructors
void takeThisAndModifyR(StandardItem &item) override { PYBIND11_OVERRIDE(void, PyPlugin, takeThisAndModifyR, item); }
void takeThisAndModifyP(StandardItem *item) override { PYBIND11_OVERRIDE(void, PyPlugin, takeThisAndModifyP, item); }
};
Then in a pyhton plugin I do:
class Plugin(Plugin):
…
def takeThisAndModifyR(self, item):
item.id = "takeThisAndModifyR";
def takeThisAndModifyP(self, item):
item.id = "takeThisAndModifyP";
…
In the app i do
auto item = StandardItem("id");
INFO << item.id();
p->takeThisAndModifyP(&item);
INFO << item.id();
p->takeThisAndModifyR(item);
INFO << item.id();
Finally this prints:
10:47:43 [info:python] id
10:47:43 [info:python] takeThisAndModifyP
10:47:43 [warn:albert] [17ms] Failed loading 'find': return_value_policy = copy, but type albert::StandardItem is non-copyable!
Of course I exposed the item class. Left it out for readability. Obviously pybind does not like references. Is this a bug or a known limitation and I should rather look for a workaround?
Related:
- https://github.com/pybind/pybind11/discussions/3991
- https://stackoverflow.com/questions/72500615/how-to-pass-arguments-by-reference-when-call-python-in-c-with-pybind11
- https://pybind11.readthedocs.io/en/stable/faq.html#limitations-involving-reference-arguments
- https://github.com/pybind/pybind11/issues/1123
Reproducible example code
No response
Is this a regression? Put the last known working version here if it is.
Not a regression
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reference-argument limitation in the linked pybind11 FAQ, then compare the behavior with discussions and issues 3991 and 1123. Reproduce the shown trampoline example using a non-copyable StandardItem and determine whether the result is expected or requires a change; done means a confirmed resolution with an appropriate regression test or documentation update.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100