Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
260b26b3
Commit
260b26b3
authored
Sep 09, 2016
by
Wenzel Jakob
Committed by
GitHub
Sep 09, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #399 from jagerman/fix-alias-initialization
Fix type alias initialization
parents
9d7f7a38
9c6859ee
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
20 deletions
+27
-20
include/pybind11/pybind11.h
+1
-1
tests/test_issues.cpp
+18
-11
tests/test_issues.py
+8
-8
No files found.
include/pybind11/pybind11.h
View file @
260b26b3
...
@@ -851,7 +851,7 @@ public:
...
@@ -851,7 +851,7 @@ public:
record
.
scope
=
scope
;
record
.
scope
=
scope
;
record
.
name
=
name
;
record
.
name
=
name
;
record
.
type
=
&
typeid
(
type
);
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
.
instance_size
=
sizeof
(
instance_type
);
record
.
init_holder
=
init_holder
;
record
.
init_holder
=
init_holder
;
record
.
dealloc
=
dealloc
;
record
.
dealloc
=
dealloc
;
...
...
tests/test_issues.cpp
View file @
260b26b3
...
@@ -192,34 +192,41 @@ void init_issues(py::module &m) {
...
@@ -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_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
);
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
{
class
OverrideTest
{
public:
public:
struct
A
{
int
value
=
99
;
};
struct
A
{
std
::
string
value
=
"hi"
;
};
int
v
;
std
::
string
v
;
A
a
;
A
a
;
explicit
OverrideTest
(
int
v
)
:
v
{
v
}
{}
explicit
OverrideTest
(
const
std
::
string
&
v
)
:
v
{
v
}
{}
virtual
int
int
_value
()
{
return
v
;
}
virtual
std
::
string
str
_value
()
{
return
v
;
}
virtual
int
&
int
_ref
()
{
return
v
;
}
virtual
std
::
string
&
str
_ref
()
{
return
v
;
}
virtual
A
A_value
()
{
return
a
;
}
virtual
A
A_value
()
{
return
a
;
}
virtual
A
&
A_ref
()
{
return
a
;
}
virtual
A
&
A_ref
()
{
return
a
;
}
};
};
class
PyOverrideTest
:
public
OverrideTest
{
class
PyOverrideTest
:
public
OverrideTest
{
public:
public:
using
OverrideTest
::
OverrideTest
;
using
OverrideTest
::
OverrideTest
;
int
int_value
()
override
{
PYBIND11_OVERLOAD
(
int
,
OverrideTest
,
int
_value
);
}
std
::
string
str_value
()
override
{
PYBIND11_OVERLOAD
(
std
::
string
,
OverrideTest
,
str
_value
);
}
// Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
// 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:
// 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_value
()
override
{
PYBIND11_OVERLOAD
(
A
,
OverrideTest
,
A_value
);
}
A
&
A_ref
()
override
{
PYBIND11_OVERLOAD
(
A
&
,
OverrideTest
,
A_ref
);
}
A
&
A_ref
()
override
{
PYBIND11_OVERLOAD
(
A
&
,
OverrideTest
,
A_ref
);
}
};
};
py
::
class_
<
OverrideTest
::
A
>
(
m2
,
"OverrideTest_A"
)
py
::
class_
<
OverrideTest
::
A
>
(
m2
,
"OverrideTest_A"
)
.
def_readwrite
(
"value"
,
&
OverrideTest
::
A
::
value
);
.
def_readwrite
(
"value"
,
&
OverrideTest
::
A
::
value
);
py
::
class_
<
OverrideTest
,
PyOverrideTest
>
(
m2
,
"OverrideTest"
)
py
::
class_
<
OverrideTest
,
PyOverrideTest
>
(
m2
,
"OverrideTest"
)
.
def
(
py
::
init
<
int
>
())
.
def
(
py
::
init
<
const
std
::
string
&
>
())
.
def
(
"
int_value"
,
&
OverrideTest
::
int
_value
)
.
def
(
"
str_value"
,
&
OverrideTest
::
str
_value
)
// .def("
int_ref", &OverrideTest::int
_ref)
// .def("
str_ref", &OverrideTest::str
_ref)
.
def
(
"A_value"
,
&
OverrideTest
::
A_value
)
.
def
(
"A_value"
,
&
OverrideTest
::
A_value
)
.
def
(
"A_ref"
,
&
OverrideTest
::
A_ref
);
.
def
(
"A_ref"
,
&
OverrideTest
::
A_ref
);
...
...
tests/test_issues.py
View file @
260b26b3
...
@@ -169,15 +169,15 @@ def test_move_fallback():
...
@@ -169,15 +169,15 @@ def test_move_fallback():
def
test_override_ref
():
def
test_override_ref
():
from
pybind11_tests.issues
import
OverrideTest
from
pybind11_tests.issues
import
OverrideTest
o
=
OverrideTest
(
42
)
o
=
OverrideTest
(
"asdf"
)
# Not allowed (see associated .cpp comment)
# Not allowed (see associated .cpp comment)
#i = o.
int
_ref()
#i = o.
str
_ref()
#assert o.
int_ref() == 42
#assert o.
str_ref() == "asdf"
assert
o
.
int_value
()
==
42
assert
o
.
str_value
()
==
"asdf"
assert
o
.
A_value
()
.
value
==
99
assert
o
.
A_value
()
.
value
==
"hi"
a
=
o
.
A_ref
()
a
=
o
.
A_ref
()
assert
a
.
value
==
99
assert
a
.
value
==
"hi"
a
.
value
=
7
a
.
value
=
"bye"
assert
a
.
value
==
7
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