Skip to content

Consider making the default policy for const T& arguments more friendly for non-copyable classes in callbacks #1241

Description

@EricCousineau-TRI

Follow-up from #1240 (#1200).

We disable copying and moving (not that moving is relevant here) for a set of virtual classes, because we would like to have a compile-time guarantee that we are not slicing classes. This generally has no issue in pybind, until callbacks are used, in which case the const T& instance may not be registered, and thus pybind tries to copy it, and then throws a runtime error.

Potential solutions:

  • Specify callback arguments' return-value-policies (per @jagerman's suggestion) when casting a function.
  • Modify the type_caster_base::cast(const T&) to check for automatic and if the class is non-copyable and non-movable, default to reference rather than copy.
    • This seems like it might be backwards-incompatible, but possibly not (as copying would be unsuccessful anyways).

My workaround for the time being will be to wrap any callbacks (Python callbacks cast into std::function<...>) that have const T& references to use const T* instead.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions