pybind / pybind/pybind11

[BUG]: Pass by reference does copy

Open
#4,417 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
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:

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

Not a regression

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.