pybind / pybind/pybind11

Implicit conversion of ints (and char const*) to handles?

Open
#1,309 13 comments 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

Issue description

Is there a reason why handles are not implicitly constructible from ints (I may well have missed something obvious...)? e.g. something like

py::object func() {
    py::object tpl = py::make_tuple(1, 2);
    return tpl[1];
}

seems to have a clear meaning but fails to compile (error: invalid conversion from ‘int’ to ‘const char*’ (there are two overloads for operator[], one taking a handle and the other a const char*)).

If char const* was also implicitly convertible to handle then the second overload would not be necessary either.

For additional confusion, 0 is implicitly convertible to a handle (an invalid one: the null pointer).

Asked on gitter (a more specific version of this) but got no reply.

Reproducible example code

Include the above-mentioned code in python_example (replacing the add function there, and the m.def(... line).

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 python_example mentioned in the issue, replacing its add function and m.def(...) line with the reproducible code to confirm the compilation error. Then inspect the handle and operator[] overloads involved, and compare the current behavior with the requested implicit conversions; done means the intended behavior and its tests are established.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, developer-experience
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 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.