# Wrapping array of booleans

**URL:** <https://discourse.itk.org/t/wrapping-array-of-booleans/2236>\
**Category:** Engineering\
**Tags:** rtk\
**Created:** [September 9, 2019, 9:40am UTC](https://discourse.itk.org/t/wrapping-array-of-booleans/2236 "2019-09-09T09:40:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![acoussat](https://discourse.itk.org/user_avatar/discourse.itk.org/acoussat/32/699_2.png) [@acoussat](https://discourse.itk.org/u/acoussat)\
**Post date:** [September 9, 2019, 9:40am UTC](https://discourse.itk.org/t/wrapping-array-of-booleans/2236/1 "2019-09-09T09:40:21Z")

</div>

Hi!

I am currently working on wrapping an RTK filter which has a `bool *` property along with a setter. The wrapped filter properly compiles but the method, when called from a Python script, produces an error (something like _the method expects a `bool *`_). I call it by passing a standard Python array (like `[True, False, True]`) as a parameter. Wrapping C arrays works correctly for other data types, such as `int *` or `float *`, but seems broken for `bool *`.

I tried to look at ITK source code in order to figure out how C arrays are wrapped, but I could not understand, and as far as I searched ITK never wraps `bool *`. I also tried to replace my `bool *` by a `std::vector<bool>` and it made the wrapped filter work.

I thus have several questions:

- According to ITK development guidelines, is it alright to wrap a `std::vector<bool>` instead of a `bool *`? (RTK tries to stick as much as possible to ITK guidelines.)
- Is there something to modify in ITK which would enable `bool *` to be properly wrapped?
- Is there any significant difference in wrapping a `std::vector` instead of a standard C array?

Sorry if the questions seem basic, I am far from being a swig expect.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [September 9, 2019, 1:17pm UTC](https://discourse.itk.org/t/wrapping-array-of-booleans/2236/2 "2019-09-09T13:17:08Z")

</div>

@matt.mccormick can best answer this question, but probably in about two weeks when he gets back to the office.

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [September 9, 2019, 1:41pm UTC](https://discourse.itk.org/t/wrapping-array-of-booleans/2236/3 "2019-09-09T13:41:49Z")

</div>

Not tested, but should be easy to add a couple of lines here:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/wrapping/SwigExtras.i#L15>

```auto
%array_functions(bool, BArray);
%array_class(bool, BArrayClass);

```

---

<div class="post-metadata">

**Author:** ![acoussat](https://discourse.itk.org/user_avatar/discourse.itk.org/acoussat/32/699_2.png) [@acoussat](https://discourse.itk.org/u/acoussat)\
**Post date:** [September 13, 2019, 1:43pm UTC](https://discourse.itk.org/t/wrapping-array-of-booleans/2236/4 "2019-09-13T13:43:32Z")

</div>

Hi,

Thank you for your kind replies!  
I have tried to implement @phcerdan’s solution, and recompiled everything from scratch, but the error message remains identical:  
`TypeError: in method '...', argument 2 of type 'bool *'`

I’m still stuck with this issue, as my understanding of Swig is very limited. Any tip would be greatly appreciated! Thank you very much.

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [September 23, 2019, 6:55pm UTC](https://discourse.itk.org/t/wrapping-array-of-booleans/2236/5 "2019-09-23T18:55:35Z")

</div>

Hi @acoussat!

> [@acoussat](#):
>
> - According to ITK development guidelines, is it alright to wrap a `std::vector<bool>` instead of a `bool *` ? (RTK tries to stick as much as possible to ITK guidelines.)
> - Is there something to modify in ITK which would enable `bool *` to be properly wrapped?
> - Is there any significant difference in wrapping a `std::vector` instead of a standard C array?

While there is already some `std::vector< bool >` wrapping support in ITK, we should use it with caution and prefer alternatives like `std::vector< unsigned char >` or other container types when possible. This is because [C++ has a special optimization for `std::vector< bool >`](https://stackoverflow.com/questions/17794569/why-is-vectorbool-not-a-stl-container),where a single bit is used to store every element. But, [other tools in the Python ecosystem, like Cython and NumPy use `unsigned char`](https://stackoverflow.com/questions/18058744/passing-a-numpy-pointer-dtype-np-bool-to-c). The ITK Python wrapping aims to work seamlessly with NumPy and friends.

That said, to instantiate a `vector< bool >`:

```auto
import itk
v = itk.vector.B()

```

`bool *`, and other raw pointers, should be avoided when wrapping in favor of `itk` or `std` containers because memory management is easier – the wrapped item will clean up its memory when it is destroyed.
