Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
260b26b3
Commit
260b26b3
authored
Sep 09, 2016
by
Wenzel Jakob
Committed by
GitHub
Sep 09, 2016
Browse files
Merge pull request #399 from jagerman/fix-alias-initialization
Fix type alias initialization
parents
9d7f7a38
9c6859ee
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
20 deletions
+27
-20
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+1
-1
tests/test_issues.cpp
tests/test_issues.cpp
+18
-11
tests/test_issues.py
tests/test_issues.py
+8
-8
No files found.
include/pybind11/pybind11.h
View file @
260b26b3
...
...
@@ -851,7 +851,7 @@ public:
record
.
scope
=
scope
;
record
.
name
=
name
;
record
.
type
=
&
typeid
(
type
);
record
.
type_size
=
sizeof
(
type
);
record
.
type_size
=
sizeof
(
detail
::
conditional_t
<
has_alias
,
type_alias
,
type
>
);
record
.
instance_size
=
sizeof
(
instance_type
);
record
.
init_holder
=
init_holder
;
record
.
dealloc
=
dealloc
;
...
...
tests/test_issues.cpp
View file @
260b26b3
...
...
@@ -192,34 +192,41 @@ void init_issues(py::module &m) {
m2
.
def
(
"get_moveissue1"
,
[](
int
i
)
->
MoveIssue1
*
{
return
new
MoveIssue1
(
i
);
},
py
::
return_value_policy
::
move
);
m2
.
def
(
"get_moveissue2"
,
[](
int
i
)
{
return
MoveIssue2
(
i
);
},
py
::
return_value_policy
::
move
);
// Issue 392: overridding reference-returning functions
// Issue
s
392
/397
: overridding reference-returning functions
class
OverrideTest
{
public:
struct
A
{
in
t
value
=
99
;
};
in
t
v
;
struct
A
{
std
::
str
in
g
value
=
"hi"
;
};
std
::
str
in
g
v
;
A
a
;
explicit
OverrideTest
(
int
v
)
:
v
{
v
}
{}
virtual
int
int
_value
()
{
return
v
;
}
virtual
in
t
&
int
_ref
()
{
return
v
;
}
explicit
OverrideTest
(
const
std
::
string
&
v
)
:
v
{
v
}
{}
virtual
std
::
string
str
_value
()
{
return
v
;
}
virtual
std
::
str
in
g
&
str
_ref
()
{
return
v
;
}
virtual
A
A_value
()
{
return
a
;
}
virtual
A
&
A_ref
()
{
return
a
;
}
};
class
PyOverrideTest
:
public
OverrideTest
{
public:
using
OverrideTest
::
OverrideTest
;
int
int
_value
()
override
{
PYBIND11_OVERLOAD
(
in
t
,
OverrideTest
,
int
_value
);
}
std
::
string
str
_value
()
override
{
PYBIND11_OVERLOAD
(
std
::
str
in
g
,
OverrideTest
,
str
_value
);
}
// Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
// to a python numeric value, since we only copy values in the numeric type caster:
// int &int_ref() override { PYBIND11_OVERLOAD(int &, OverrideTest, int_ref); }
// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
// But we can work around it like this:
private:
std
::
string
_tmp
;
std
::
string
str_ref_helper
()
{
PYBIND11_OVERLOAD
(
std
::
string
,
OverrideTest
,
str_ref
);
}
public:
std
::
string
&
str_ref
()
override
{
return
_tmp
=
str_ref_helper
();
}
A
A_value
()
override
{
PYBIND11_OVERLOAD
(
A
,
OverrideTest
,
A_value
);
}
A
&
A_ref
()
override
{
PYBIND11_OVERLOAD
(
A
&
,
OverrideTest
,
A_ref
);
}
};
py
::
class_
<
OverrideTest
::
A
>
(
m2
,
"OverrideTest_A"
)
.
def_readwrite
(
"value"
,
&
OverrideTest
::
A
::
value
);
py
::
class_
<
OverrideTest
,
PyOverrideTest
>
(
m2
,
"OverrideTest"
)
.
def
(
py
::
init
<
int
>
())
.
def
(
"
int
_value"
,
&
OverrideTest
::
int
_value
)
// .def("
int
_ref", &OverrideTest::
int
_ref)
.
def
(
py
::
init
<
const
std
::
string
&
>
())
.
def
(
"
str
_value"
,
&
OverrideTest
::
str
_value
)
// .def("
str
_ref", &OverrideTest::
str
_ref)
.
def
(
"A_value"
,
&
OverrideTest
::
A_value
)
.
def
(
"A_ref"
,
&
OverrideTest
::
A_ref
);
...
...
tests/test_issues.py
View file @
260b26b3
...
...
@@ -169,15 +169,15 @@ def test_move_fallback():
def
test_override_ref
():
from
pybind11_tests.issues
import
OverrideTest
o
=
OverrideTest
(
42
)
o
=
OverrideTest
(
"asdf"
)
# Not allowed (see associated .cpp comment)
#i = o.
int
_ref()
#assert o.
int
_ref() ==
42
assert
o
.
int
_value
()
==
42
#i = o.
str
_ref()
#assert o.
str
_ref() ==
"asdf"
assert
o
.
str
_value
()
==
"asdf"
assert
o
.
A_value
().
value
==
99
assert
o
.
A_value
().
value
==
"hi"
a
=
o
.
A_ref
()
assert
a
.
value
==
99
a
.
value
=
7
assert
a
.
value
==
7
assert
a
.
value
==
"hi"
a
.
value
=
"bye"
assert
a
.
value
==
"bye"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment