[BUG]: implicitly_convertible with templated constructor
维护者通常 4 天内回复
还没有人认领这个 Issue。
评估
调研方向
首先跟踪绑定示例中使用的入口点 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
-
[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 天内回复