Skip to content

Commit

Permalink
Add support for vector[bool].at(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwb committed Oct 6, 2016
1 parent 338d5c4 commit 73db250
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Cython/Compiler/PyrexTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3478,6 +3478,19 @@ def specialize(self, values):
if self.namespace is not None:
specialized.namespace = self.namespace.specialize(values)
specialized.scope = self.scope.specialize(values, specialized)
if self.cname == 'std::vector':
# vector<bool> is special cased in the C++ standard, and its
# accessors do not necessarily return references to the underlying
# elements (which may be bit-packed).
# http://www.cplusplus.com/reference/vector/vector-bool/
# Here we pretend that the various methods return bool values
# (as the actual returned values are coercable to such, and
# we don't support call expressions as lvalues).
T = values[self.templates[0]]
if T.empty_declaration_code() == 'bool':
for bit_ref_returner in ('at', 'back', 'front'):
if bit_ref_returner in specialized.scope.entries:
specialized.scope.entries[bit_ref_returner].type.return_type = T
return specialized

def deduce_template_params(self, actual):
Expand Down
3 changes: 2 additions & 1 deletion tests/run/cpp_stl_vector.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def test_bool_vector_get_set():
# Test access.
assert not v[0], v
assert v[1], v
# assert v.at(0)
assert not v.at(0), v
assert v.at(1), v
v[0] = True
v[1] = False
assert <object>v == [True, False, True, True, True]

0 comments on commit 73db250

Please sign in to comment.