mirror of https://github.com/pybind/pybind11
Browse Source
In def_readonly and def_readwrite, there is an assertion that the member comes from the class or a base class: static_assert(std::is_base_of<C, type>::value, "..."); However, if C and type are the same type, is_base_of will still only be true if they are the same _non-union_ type. This means we can't define accessors for the members of a union type because of this assertion. Update the assertion to test std::is_same<C, type>::value || std::is_base_of<C, type>::value which will allow union types, or members of base classes. Also add a basic unit test for accessing unions.pull/1748/head
4 changed files with 33 additions and 2 deletions
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
tests/test_class.cpp -- test py::class_ definitions and basic functionality |
||||
|
||||
Copyright (c) 2019 Roland Dreier <roland.dreier@gmail.com> |
||||
|
||||
All rights reserved. Use of this source code is governed by a |
||||
BSD-style license that can be found in the LICENSE file. |
||||
*/ |
||||
|
||||
#include "pybind11_tests.h" |
||||
|
||||
TEST_SUBMODULE(union_, m) { |
||||
union TestUnion { |
||||
int value_int; |
||||
unsigned value_uint; |
||||
}; |
||||
|
||||
py::class_<TestUnion>(m, "TestUnion") |
||||
.def(py::init<>()) |
||||
.def_readonly("as_int", &TestUnion::value_int) |
||||
.def_readwrite("as_uint", &TestUnion::value_uint); |
||||
} |
Loading…
Reference in new issue