Cannot declare vector<bool> properties on OSX


#1

Hi,

When I try to declare a property of type vector using RTTI_PROPERTY I get a compiler message on Xcode:
cannot bind to a temporary of type '__bit_const_reference<…>
See below for a trace report. What I suspect is that vector is internally handled with bit masks by STL and this creates problems because there are no actual ‘bools’. In that case it could be a toughie…

Does anyone have an idea how to fix this or for a workaround? Thanks!

Stijn.

/Users/macbook/documents/Repositories/thirdparty/pybind11/install/include/pybind11/stl.h:163:20: Non-const lvalue reference to type ‘__bit_const_reference<…>’ cannot bind to a temporary of type ‘__bit_const_reference<…>’
/Users/macbook/Documents/Repositories/4dsound_source/modules/napaudioadvanced/src/audio/core/multiobject.cpp:1:10: In file included from /Users/macbook/Documents/Repositories/4dsound_source/modules/napaudioadvanced/src/audio/core/multiobject.cpp:1:
/Users/macbook/Documents/Repositories/4dsound_source/modules/napaudioadvanced/src/audio/core/multiobject.h:4:10: In file included from /Users/macbook/Documents/Repositories/4dsound_source/modules/napaudioadvanced/src/audio/core/multiobject.h:4:
/Users/macbook/documents/Repositories/4dsound_source/modules/napscene/src/component.h:3:10: In file included from /Users/macbook/documents/Repositories/4dsound_source/modules/napscene/src/component.h:3:
/Users/macbook/documents/Repositories/4dsound_source/rtti/src/rtti/object.h:4:10: In file included from /Users/macbook/documents/Repositories/4dsound_source/rtti/src/rtti/object.h:4:
/Users/macbook/documents/Repositories/4dsound_source/rtti/src/rtti/rtti.h:3:10: In file included from /Users/macbook/documents/Repositories/4dsound_source/rtti/src/rtti/rtti.h:3:
/Users/macbook/documents/Repositories/4dsound_source/rtti/src/rtti/typeinfo.h:6:11: In file included from /Users/macbook/documents/Repositories/4dsound_source/rtti/src/rtti/typeinfo.h:6:
/Users/macbook/documents/Repositories/4dsound_source/rtti/src/rtti/pythonmodule.h:9:10: In file included from /Users/macbook/documents/Repositories/4dsound_source/rtti/src/rtti/pythonmodule.h:9:
/Users/macbook/documents/Repositories/thirdparty/pybind11/install/include/pybind11/pybind11.h:157:39: In instantiation of function template specialization ‘pybind11::detail::list_caster<std::__1::vector<bool, std::__1::allocator >, bool>::cast<const std::__1::vector<bool, std::__1::allocator > &>’ requested here
/Users/macbook/documents/Repositories/thirdparty/pybind11/install/include/pybind11/pybind11.h:69:9: In instantiation of function template specialization ‘pybind11::cpp_function::initialize<(lambda at /Users/macbook/documents/Repositories/thirdparty/pybind11/install/include/pybind11/pybind11.h:1072:27), const std::__1::vector<bool, std::_1::allocator > &, const nap::audio::MultiObject &, pybind11::is_method>’ requested here
/Users/macbook/documents/Repositories/thirdparty/pybind11/install/include/pybind11/pybind11.h:1072:22: In instantiation of function template specialization ‘pybind11::cpp_function::cpp_function<(lambda at /Users/macbook/documents/Repositories/thirdparty/pybind11/install/include/pybind11/pybind11.h:1072:27), pybind11::is_method, void>’ requested here
/Users/macbook/Documents/Repositories/4dsound_source/modules/napaudioadvanced/src/audio/core/multiobject.cpp:15:5: In instantiation of function template specialization 'pybind11::class
<nap::audio::MultiObject, nap::audio::AudioObject>::def_readonly<nap::audio::MultiObject, std::__1::vector<bool, std::__1::allocator >>’ requested here
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:2301:20: Selected ‘begin’ function with iterator type ‘const_iterator’ (aka ‘__bit_iterator<std::__1::vector<bool, std::__1::allocator >, true>’)


#2

Can you post the exact use case, ie: member declaration and definition in .h and .cpp?

From what I know declaring a vector hasn’t caused any previous issues but I never declared an array of boolean values. I suspect using an int is ok? We do that pretty much all the time. An int can be cast to a boolean.

Also: please post what version of nap you are using and if you are using the latest (unstable) source build. I will add a ticket.


#3

Vector of int works fine as always. Think this is the first time I am trying a vector too.
in the resource class:

std::vector<bool> mActive = { true };

in the RTTI definition:

RTTI_PROPERTY("Active", &nap::audio::MultiChain::mActive, nap::rtti::EPropertyMetaData::Default)

I am working with the NAP version that is in the current nap_source repo, running OSX. Cheers!

Stijn.


#4

The STL replaces std::vector<bool> with a bit set… Saves some memory but is otherwise just annoying at times to deal with. One of the consequences is, you cannot have pointers or references to elements of type bool (e.g. bool & or bool *). I suppose that’s why it won’t compile. Perhaps use std::vector<T>::reference instead of T& in affected places.

See http://www.cplusplus.com/reference/vector/vector-bool/.


#5

Exactly that’s what I suspect too Marcel, By the way I noticed the <bool> bit was formatted away from my first post because of html formatting, hence the unclarity. :slight_smile:


#6

Just looked into it a bit deeper and it breaks in pybind11/stl.h where the boolean vector gets exposed to the python object.


#7

Briefly discussed this with Jelle / Ritesh and there is no quick fix for this issue. @marcel makes a valid point, it’s not supported and hard to support with the given rttr / python implementation. So for now I recommend using an alternative or add support for it.