[BUG]: implicitly_convertible with templated constructor

Offen
#5,271 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 4 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
45/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Ruhig
Tech-Stack
cpp, python
Bereich
api

Rechercherichtung

Beginne damit, die in dem Binding-Beispiel verwendeten Einstiegspunkte py::implicitly_convertible<B, A>() und py::implicitly_convertible<C, A>() nachzuverfolgen, und reproduziere anschließend den Fehler mit den gezeigten Typen A, B und C. Erledigt ist die Aufgabe, wenn Aufrufe von m.def("print", &print) sowohl example.B als auch example.C akzeptieren und ihr jeweiliges print-Verhalten beibehalten, mit Testabdeckung für Template-Konstruktoren.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

triage
Required prerequisites
What version (or hash if on master) of pybind11 are you using?

2.13.1

Problem description

Could we make implicitly_convertible explicitly instantiate templated constructors?

Consider this type erasure pattern (remark the template constructor of A for implicit conversion).

#pragma once

#include <memory>
#include <iostream>

// Concept and Model classes for type erasure
namespace detail {
    struct Concept {
        virtual ~Concept() = default;
        virtual void print() const = 0;
        virtual std::unique_ptr<Concept> clone() const = 0;
    };

    template <typename T>
    struct Model : Concept {
        T data;
        Model(T const& data) : data(data) {}
        void print() const override { data.print(); }
        std::unique_ptr<Concept> clone() const override { return std::make_unique<Model<T>>(*this); }
    };
}

// Type-Erased Class A
class A {
    std::unique_ptr<detail::Concept> pimpl;

public:
    template <typename T>
    A(T const& x) : pimpl(std::make_unique<detail::Model<T>>(x)) {}

    A(A const& other) : pimpl(other.pimpl->clone()) {}
    A& operator=(A const& other) { pimpl = other.pimpl->clone(); return *this; }
    A(A&& other) noexcept = default;
    A& operator=(A&& other) noexcept = default;

    void print() const { pimpl->print(); }
};

// Specialized Class B
class B {
public:
    void print() const {
        std::cout << "I am B" << std::endl;
    }
};

// Specialized Class C
class C {
public:
    void print() const {
        std::cout << "I am C" << std::endl;
    }
};

void print(const A& foo) {
   foo.print();
}

The python binding:

PYBIND11_MODULE(example, m) {
    py::class_<A>(m, "A")
        .def("print", &A::print);

    py::class_<B>(m, "B")
        .def(py::init<>())
        .def("print", &B::print);

    py::class_<C>(m, "C")
        .def(py::init<>())
        .def("print", &C::print);

    py::implicitly_convertible<B, A>();
    py::implicitly_convertible<C, A>();
    
    m.def("print", &print);
}

I would expect the python binding to behave like:

import example

b = example.B()
print_from_b = example.print(b)  # This should print "I am B" due to implicit conversion

c = example.C()
print_from_c = example.print(c)  # This should print "I am C" due to implicit conversion

The error traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: printA(): incompatible function arguments. The following argument types are supported:
    1. (arg0: example.A) -> None

Invoked with: <example.B object at 0x75f99e0c3130>
Reproducible example code

No response

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

Not a regression

Vorherrschende Sprache
C++
Sterne
18k
Forks
2.3k
Ø Merge
5 T. 7 Std.
Gemergte PRs (30 T.)
53

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus pybind/pybind11

Alle Issues in pybind/pybind11

Ähnliche Issues

Weitere Issues zu C++

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.