vector<int> vec({1, 1, 2, 1});
int *some_int = &vec[2];
*some_int = 1;
Duh. So simple code. And it works. So now let's try bools instead:
vector<bool> vec({false, false, true, false});
bool *some_bool = &vec[2];
*some_bool = false;
No luck. The compiler spits out errors (ideone):
prog.cpp: In function ‘int main()’:
prog.cpp:8:26: error: taking address of temporary [-fpermissive]
bool *some_bool = &vec[2];
^
prog.cpp:8:26: error: cannot convert ‘std::vector<bool>::reference* {aka std::_Bit_reference*}’ to ‘bool*’ in initialization
Maybe let's try references?
vector<bool> vec({false, false, true, false});
bool &some_bool = vec[2];
some_bool = false;
No luck either (ideone):
prog.cpp: In function ‘int main()’:
prog.cpp:8:25: error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘bool’
bool &some_bool = vec[2];
~~~~~^
In file included from /usr/include/c++/6/vector:65:0,
from prog.cpp:2:
/usr/include/c++/6/bits/stl_bvector.h:80:5: note: after user-defined conversion: std::_Bit_reference::operator bool() const
operator bool() const _GLIBCXX_NOEXCEPT
Dang. Is it impossible to store the reference or pointer to a member of std::vector<bool>
in a different place of code?! I need this for my data structure.
How to fix this issue? Is switching from std::vector<bool>
to std::vector<int>
the only option?