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
5dd33d88
Commit
5dd33d88
authored
May 30, 2016
by
Wenzel Jakob
Browse files
fix issues with std::vector<bool> overload in STL (fixes #216)
parent
dca6b04c
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
19 additions
and
5 deletions
+19
-5
example/example17.cpp
example/example17.cpp
+1
-0
example/example17.py
example/example17.py
+9
-1
example/example17.ref
example/example17.ref
+1
-0
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+5
-2
include/pybind11/stl_bind.h
include/pybind11/stl_bind.h
+3
-2
No files found.
example/example17.cpp
View file @
5dd33d88
...
...
@@ -29,6 +29,7 @@ void init_ex17(py::module &m) {
.
def
(
pybind11
::
init
<
int
>
());
pybind11
::
bind_vector
<
unsigned
int
>
(
m
,
"VectorInt"
);
pybind11
::
bind_vector
<
bool
>
(
m
,
"VectorBool"
);
pybind11
::
bind_vector
<
El
>
(
m
,
"VectorEl"
);
...
...
example/example17.py
View file @
5dd33d88
#!/usr/bin/env python
from
__future__
import
print_function
from
example
import
VectorInt
,
El
,
VectorEl
,
VectorVectorEl
from
example
import
VectorInt
,
El
,
VectorEl
,
VectorVectorEl
,
VectorBool
v_int
=
VectorInt
([
0
,
0
])
print
(
len
(
v_int
))
...
...
@@ -38,3 +38,11 @@ vv_a = VectorVectorEl()
vv_a
.
append
(
v_a
)
vv_b
=
vv_a
[
0
]
print
(
vv_b
)
vv_c
=
VectorBool
()
for
i
in
range
(
10
):
vv_c
.
append
(
i
%
2
==
0
)
for
i
in
range
(
10
):
if
vv_c
[
i
]
!=
(
i
%
2
==
0
):
print
(
"Error!"
)
print
(
vv_c
)
example/example17.ref
View file @
5dd33d88
...
...
@@ -8,3 +8,4 @@ VectorInt[3, 0, 99, 2, 3]
VectorInt[0, 99, 2, 3]
VectorEl[El{1}, El{2}]
VectorEl[El{1}, El{2}]
VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
include/pybind11/pybind11.h
View file @
5dd33d88
...
...
@@ -1019,13 +1019,16 @@ NAMESPACE_END(detail)
template
<
typename
...
Args
>
detail
::
init
<
Args
...
>
init
()
{
return
detail
::
init
<
Args
...
>
();
}
template
<
typename
Iterator
,
typename
...
Extra
>
iterator
make_iterator
(
Iterator
first
,
Iterator
last
,
Extra
&&
...
extra
)
{
template
<
typename
Iterator
,
typename
ValueType
=
decltype
(
*
std
::
declval
<
Iterator
>()),
typename
...
Extra
>
iterator
make_iterator
(
Iterator
first
,
Iterator
last
,
Extra
&&
...
extra
)
{
typedef
detail
::
iterator_state
<
Iterator
>
state
;
if
(
!
detail
::
get_type_info
(
typeid
(
state
)))
{
class_
<
state
>
(
handle
(),
""
)
.
def
(
"__iter__"
,
[](
state
&
s
)
->
state
&
{
return
s
;
})
.
def
(
"__next__"
,
[](
state
&
s
)
->
decltype
(
*
std
::
declval
<
Iterator
>
())
{
.
def
(
"__next__"
,
[](
state
&
s
)
->
ValueType
{
if
(
s
.
it
==
s
.
end
)
throw
stop_iteration
();
return
*
s
.
it
++
;
...
...
include/pybind11/stl_bind.h
View file @
5dd33d88
...
...
@@ -136,6 +136,7 @@ pybind11::class_<std::vector<T, Allocator>, holder_type> bind_vector(pybind11::m
using
Vector
=
std
::
vector
<
T
,
Allocator
>
;
using
SizeType
=
typename
Vector
::
size_type
;
using
DiffType
=
typename
Vector
::
difference_type
;
using
ItType
=
typename
Vector
::
iterator
;
using
Class_
=
pybind11
::
class_
<
Vector
,
holder_type
>
;
Class_
cl
(
m
,
name
.
c_str
(),
std
::
forward
<
Args
>
(
args
)...);
...
...
@@ -214,7 +215,7 @@ pybind11::class_<std::vector<T, Allocator>, holder_type> bind_vector(pybind11::m
);
cl
.
def
(
"__getitem__"
,
[](
const
Vector
&
v
,
SizeType
i
)
{
[](
const
Vector
&
v
,
SizeType
i
)
->
T
{
if
(
i
>=
v
.
size
())
throw
pybind11
::
index_error
();
return
v
[
i
];
...
...
@@ -242,7 +243,7 @@ pybind11::class_<std::vector<T, Allocator>, holder_type> bind_vector(pybind11::m
cl
.
def
(
"__iter__"
,
[](
Vector
&
v
)
{
return
pybind11
::
make_iterator
(
v
.
begin
(),
v
.
end
());
return
pybind11
::
make_iterator
<
ItType
,
T
>
(
v
.
begin
(),
v
.
end
());
},
pybind11
::
keep_alive
<
0
,
1
>
()
/* Essential: keep list alive while iterator exists */
);
...
...
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