[BUG]: implicitly_convertible with templated constructor
メンテナーはふだん 4 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
まず、バインディング例で使用されているエントリポイント py::implicitly_convertible<B, A>() と py::implicitly_convertible<C, A>() を追跡し、次に示されている型 A、B、C で失敗を再現します。m.def("print", &print) の呼び出しが example.B と example.C の両方を受け付け、それぞれの print の動作を維持し、テンプレートコンストラクタをカバーできれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
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
- 主要言語
- C++
- スター
- 18k
- フォーク
- 2.3k
- 平均マージ
- 5日 7時間
- マージ済み PR(30日)
- 53
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
pybind/pybind11 のほかの issue
-
triage
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 4 日以内に返信
-
triage
難易度 1/5 1時間未満 初心者へのやさしさ 62/100
pybind/pybind11#5604 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
-
triage
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 4 日以内に返信
-
docs
難易度 2/5 1〜3時間 初心者へのやさしさ 60/100
pybind/pybind11#747 · コメント 2 件 ·
メンテナーはふだん 4 日以内に返信
-
array_t::at() returns a misaligned reference when the stride is not a multiple of alignof(T)対応中かも @henryiii が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
メンテナーはふだん 4 日以内に返信
pybind/pybind11 の issue をすべて見る
似ている issue
-
[Bug] Windows Ghidra provider rejects a PATH-only JDK: bare `java.exe` probe fails with Win32 87オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
type:bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
tenstorrent/tt-umd#4084 ·
メンテナーはふだん 5 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
ToffelsKater/X4_VR#34 ·
-
difficulty:easy prio:low refactor
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
NatronGitHub/openfx-supportext#220 ·
メンテナーはふだん 1 日以内に返信