Skip to content

Commit 260b26b

Browse files
authored
Merge pull request #399 from jagerman/fix-alias-initialization
Fix type alias initialization
2 parents 9d7f7a3 + 9c6859e commit 260b26b

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

include/pybind11/pybind11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ class class_ : public detail::generic_type {
851851
record.scope = scope;
852852
record.name = name;
853853
record.type = &typeid(type);
854-
record.type_size = sizeof(type);
854+
record.type_size = sizeof(detail::conditional_t<has_alias, type_alias, type>);
855855
record.instance_size = sizeof(instance_type);
856856
record.init_holder = init_holder;
857857
record.dealloc = dealloc;

tests/test_issues.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,34 +192,41 @@ void init_issues(py::module &m) {
192192
m2.def("get_moveissue1", [](int i) -> MoveIssue1 * { return new MoveIssue1(i); }, py::return_value_policy::move);
193193
m2.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
194194

195-
// Issue 392: overridding reference-returning functions
195+
// Issues 392/397: overridding reference-returning functions
196196
class OverrideTest {
197197
public:
198-
struct A { int value = 99; };
199-
int v;
198+
struct A { std::string value = "hi"; };
199+
std::string v;
200200
A a;
201-
explicit OverrideTest(int v) : v{v} {}
202-
virtual int int_value() { return v; }
203-
virtual int &int_ref() { return v; }
201+
explicit OverrideTest(const std::string &v) : v{v} {}
202+
virtual std::string str_value() { return v; }
203+
virtual std::string &str_ref() { return v; }
204204
virtual A A_value() { return a; }
205205
virtual A &A_ref() { return a; }
206206
};
207207
class PyOverrideTest : public OverrideTest {
208208
public:
209209
using OverrideTest::OverrideTest;
210-
int int_value() override { PYBIND11_OVERLOAD(int, OverrideTest, int_value); }
210+
std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
211211
// Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
212212
// to a python numeric value, since we only copy values in the numeric type caster:
213-
// int &int_ref() override { PYBIND11_OVERLOAD(int &, OverrideTest, int_ref); }
213+
// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
214+
// But we can work around it like this:
215+
private:
216+
std::string _tmp;
217+
std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
218+
public:
219+
std::string &str_ref() override { return _tmp = str_ref_helper(); }
220+
214221
A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
215222
A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
216223
};
217224
py::class_<OverrideTest::A>(m2, "OverrideTest_A")
218225
.def_readwrite("value", &OverrideTest::A::value);
219226
py::class_<OverrideTest, PyOverrideTest>(m2, "OverrideTest")
220-
.def(py::init<int>())
221-
.def("int_value", &OverrideTest::int_value)
222-
// .def("int_ref", &OverrideTest::int_ref)
227+
.def(py::init<const std::string &>())
228+
.def("str_value", &OverrideTest::str_value)
229+
// .def("str_ref", &OverrideTest::str_ref)
223230
.def("A_value", &OverrideTest::A_value)
224231
.def("A_ref", &OverrideTest::A_ref);
225232

tests/test_issues.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ def test_move_fallback():
169169

170170
def test_override_ref():
171171
from pybind11_tests.issues import OverrideTest
172-
o = OverrideTest(42)
172+
o = OverrideTest("asdf")
173173

174174
# Not allowed (see associated .cpp comment)
175-
#i = o.int_ref()
176-
#assert o.int_ref() == 42
177-
assert o.int_value() == 42
175+
#i = o.str_ref()
176+
#assert o.str_ref() == "asdf"
177+
assert o.str_value() == "asdf"
178178

179-
assert o.A_value().value == 99
179+
assert o.A_value().value == "hi"
180180
a = o.A_ref()
181-
assert a.value == 99
182-
a.value = 7
183-
assert a.value == 7
181+
assert a.value == "hi"
182+
a.value = "bye"
183+
assert a.value == "bye"

0 commit comments

Comments
 (0)