pybind11 联合失败'is_base_of'断言
pybind11 union fails 'is_base_of' assert
我正在尝试将联合与pybind11绑定,但出现一个奇怪的错误。我不确定我是否达到了 pybind11 的限制,或者我是否正确地实现了这个。
有人能指出我正确的方向吗?谢谢!
#include <iostream>
#include <pybind11/pybind11.h>
namespace py = pybind11;
struct object {
object();
union union_type {
bool boolean;
uint64_t u64;
int64_t i64;
double f64;
};
union_type via;
};
PYBIND11_MODULE(example, m) {
pybind11::class_<object, std::shared_ptr<object>> cl(m, "object", "Object class that corresponding stuff");
pybind11::handle cl_type = cl;
{
auto & enclosing_class = cl;
py::class_<object::union_type, std::shared_ptr<object::union_type>> cl(enclosing_class, "union_type", "");
cl.def( pybind11::init( [](object::union_type const &o){ return new object::union_type(o); } ) );
cl.def( pybind11::init( [](){ return new object::union_type(); } ) );
cl.def_readwrite("boolean", &object::union_type::boolean);
cl.def_readwrite("u64", &object::union_type::u64);
cl.def_readwrite("i64", &object::union_type::i64);
cl.def_readwrite("f64", &object::union_type::f64);
cl.def("assign", (union object::union_type & (object::union_type::*)(const union object::union_type &)) &object::union_type::operator=, "C++: object::union_type::operator=(const union object::union_type &) --> union object::union_type &", pybind11::return_value_policy::automatic, pybind11::arg(""));
}
}
错误:
pybind11/pybind11.h: In instantiation of ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_readwrite(const char*, D C::*, const Extra& ...)
[with C = object::union_type; D = bool; Extra = {}; type_ = object::union_type; options = {std::shared_ptr<object::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)");
^~~~~~~~~~~~~
编辑 - 我编译:
c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` mytest.cpp -o example`python3-config --extension-suffix`
根据波拉科维奇建议的一般工会问题,您可以申请:
typedef union _union_type {
bool boolean;
uint64_t u64;
int64_t i64;
double f64;
}union_type;
py::class_<union_type>(m, "union_type")
.def(py::init<>())
.def_property("boolean",
[](union_type& self) -> const bool&
{
return self.boolean;
},
[](union_type& self, const bool& value)
{
self.boolean = value;
});
.def_property("u64",
[](union_type& self) -> const uint64_t&
{
return self.u64;
},
[](union_type& self, const uint64_t& value)
{
self.u64 = value;
});
.def_property("i64",
[](union_type& self) -> const int64_t&
{
return self.i64;
},
[](union_type& self, const int64_t& value)
{
self.i64 = value;
});
.def_property("f64",
[](union_type& self) -> const double&
{
return self.f64;
},
[](union_type& self, const int64_t& value)
{
self.f64 = value;
});
相关文章:
- Usages of std::move
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 构造函数采用 Base&不被调用
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 具有变量Number of Arguments的std::函数的矢量
- Capacity of a deque
- Deprecation of _writeBarrier()
- constexpr begin of a std::array
- 尝试应用肖恩父母谈话"inheritance is the base class of evil"中的模式
- C++中的继承,"...is an ambiguous base of ..."错误
- "base operand of ‘->’ has non-pointer type" 但它是指针
- 通过"a pointer of the base class"访问未在基类中声明的子类的方法或属性(动态)
- 为什么在使用dynamic_cast和模板时出现错误"A is an inaccessible base of B"?
- "virtual base class in the case of multilevel inheritance"有意义吗
- C++错误,显示创建链接列表调用"error LinkedList Interface is an inaccessable base of linkedlist"
- 如何在C++实现继承并解决错误"parent class is not accessible base of child class"?
- 关于肖恩·帕伦特谈话的澄清 "Inheritance is the base class of evil"
- 我怎么能说"noexcept if execution of protected base constructor is noexcept"?
- "Is not a direct base of " GCC 4.5.2 编译器错误