[BUG]: implicitly_convertible with templated constructor
維護者通常 4 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
首先追蹤 binding 範例中使用的進入點 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 小時
- 30 天內合併 PR
- 53
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 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
維護者通常 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 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 70/100
維護者通常 1 天內回覆
-
[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 ·