PYBIND11_NUMPY_DTYPE for union struct member - cannot compute offset of bit field
Open
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Hi,
I want to bind an array of union, say I have:
typedef union A_t {
uint32_t full;
struct {
uint32_t x:16;
uint32_t y:16;
}f;
} A_t;
union B_t {
struct {
A_t regs[10];
} r ;
} B_t;
if I want to create a pybind11::array of that union I'd have to declare its DTYPE:
PYBIND11_NUMPY_DTYPE(A_t, full, f.x, f.y)
py::class_<B_t>(m, "B_t")
.def(py::init<>())
.def_property("regs", [](B_t &self) -> pybind11::array {
auto dtype = py::dtype(pybind11::format_descriptor<A_t>::format());
auto base = py::array(dtype, {10}, {sizeof(A_t)});
return py::array(
dtype, {10}, {sizeof(A_t)}, self.r.regs, base);
}, [](B_t& ) {})
but I'm getting error:
error: cannot compute offset of bit-field 'x'.
Any suggestions on how to do that?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the error with the shown A_t and B_t definitions and the PYBIND11_NUMPY_DTYPE declaration. Trace the dtype registration path that reports it cannot compute the offset of bit-field x, then determine whether union bit-fields can be supported or the limitation should be documented. Done means the behavior is resolved or clearly specified with a verified example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, numpy, python
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100