pybind / pybind/pybind11

[BUG] pybind11 fails to distinguish template instantiations with auto template parameter

Open
#2,913 2 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

pybind11 fails to distinguish template <auto D> class ... instantiations, and report

ImportError: generic_type: type "TestClass6" is already registered!

using template <size_t D> class ... works without issue.

Tested on MSVC 16.9.1, Python 3.8.3, pybind11 2.6.2

If it's unavoidable, is there any workaround?

Reproducible example code

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

template <auto D>
class TestClass {
public:
    void test(std::array<double, D>& a) {
        a[0] = 1;
    }
};
PYBIND11_MODULE(vtc, m) {
    m.doc() = "Vehicle tracking control Python binding";

    using TestClass6 = TestClass<6>;
    py::class_<TestClass6>(m, "TestClass6")
        .def("test", &TestClass6::test);

    using TestClass8 = TestClass<8>;
    py::class_<TestClass8>(m, "TestClass8")
        .def("test", &TestClass8::test);
}

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 building and running the reproducible module shown in the issue with the two auto-parameter instantiations, then inspect the py::class_ registration path included from pybind11/pybind11.h. Compare how TestClass6 and TestClass8 are identified during registration with the size_t version. Done means both classes import without a duplicate-registration error.

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.