Skip to content

union fails 'is_base_of' assert #1685

Closed
@danpf

Description

@danpf

I'm trying to bind a union with pybind11 and I'm getting a strange error. I'm not sure if i'm hitting a limitation of pybind11, or i'm implementing this incorrectly. Could anyone let me know if I'm implementing this incorrectly or if this is a limitation of pybind11? It looks like it is failing on

cl.def_readwrite("boolean", &union_type::boolean);

I'll note that I'm getting a similar error with

Issue description

union fails 'is_base_of' assert

Reproducible example code


#include <iostream>
#include <pybind11/pybind11.h>

namespace py = pybind11;

union union_type {
	 bool boolean;
	 uint64_t u64;
	 int64_t  i64;
	 double   f64;
};

PYBIND11_MODULE(example, m) {
	union_type a;
	py::class_<union_type> cl(m, "union_type", "");
	cl.def( pybind11::init( [](union_type const &o){ return new union_type(o); } ) );
	cl.def( pybind11::init( [](){ return new union_type(); } ) );
	cl.def_readwrite("boolean", &union_type::boolean);
	cl.def_readwrite("u64", &union_type::u64);
	cl.def_readwrite("i64", &union_type::i64);
	cl.def_readwrite("f64", &union_type::f64);
}

Error

pybind11/pybind11.h: In instantiation of ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_readwrite(const char*, D C::*, const Extra& ...) \
[with C = union_type; D = bool; Extra = {}; type_ = union_type; options = {std::shared_ptr<union_type>}]’:
mytest.cpp:35:59:   required from here
python3.7m/pybind11/pybind11.h:1161:9: error: static assertion failed: def_readwrite() requires a class member (or base class member
)
         static_assert(std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)");
         ^~~~~~~~~~~~~

compiled with:

c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` mytest.cpp -o example`python3-config --extension-suffix`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions