pybind / pybind/pybind11

[QUESTION]Can different return value and call policies be used depending on the actual type of std::variant?

Open
#3,089 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Recently, I want to export a function whose return type is std::variant. However, different return value policies and additional call policies should be applied according to the actual return value type, for example:
When return type is A, it needs to be py::return_value_policy::copy.
When return type is B, it needs to be py::return_value_policy::take_ownership with additional call policy py::keep_alive<0, 1>().
When return type is C, it needs to be py::return_value_policy::reference_internal.

Are there any ways to specify the different return_value_policies for each return type of std::variant<...>? Many thanks!

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 by reviewing pybind11's std::variant conversion path and the return_value_policy and keep_alive APIs named in the question. Done means establishing whether policies can vary by variant alternative and documenting or specifying the required supported approach.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.