#include <vector>
struct A
{
void foo(){}
};
template< typename T >
void callIfToggled( bool v1, bool &v2, T & t )
{
if ( v1 != v2 )
{
v2 = v1;
t.foo();
}
}
int main()
{
std::vector< bool > v= { false, true, false };
const bool f = false;
A a;
callIfToggled( f, v[0], a );
callIfToggled( f, v[1], a );
callIfToggled( f, v[2], a );
}
The compilation of the example above produces next error :
dk2.cpp: In function 'int main()':
dk2.cpp:29:28: error: no matching function for call to 'callIfToggled(const bool&, std::vector<bool>::reference, A&)'
dk2.cpp:29:28: note: candidate is:
dk2.cpp:13:6: note: template<class T> void callIfToggled(bool, bool&, T&)
I compiled using g++ (version 4.6.1) like this :
g++ -O3 -std=c++0x -Wall -Wextra -pedantic dk2.cpp
The question is why this happens? Is vector<bool>::reference
not bool&
? Or is it a compiler's bug?
Or, am I trying something stupid? :)
Vector is specialized for bool.
It is considered a mistake of the std. Use
vector<char>
instead:Occasionally you may need references to a bool contained inside the vector. Unfortunately, using
vector<char>
can only give you references to chars. If you really needbool&
, check out the Boost Containers library. It has an unspecialized version ofvector<bool>
.std::vector<bool>
is a non conforming container. To optimize space, it packsbool
s and cannot provide reference.Use
boost::dynamic_bitset
instead.Your expectations are normal, but the problem is that
std::vector<bool>
has been a kind of experiment by the C++ commitee. It is actually a template specialization that stores the bool values tightly packed in memory: one bit per value.And since you cannot have a reference to a bit, there's your problem.
Just my 2 cents:
std::vector<bool>::reference
is a typedef forstruct _Bit_reference
which is defined asChanging the function like this, it works (well, compiles at least, haven't tested):
EDIT: I changed the condition from (v1 != v2), which wasn't a good idea, to (v1 != b).
Make a structure with a
bool
in it, and make thevector<>
using that struct type.Try:
vector<struct sb>
wheresb
isstruct {boolean b];
then you can say
push_back({true})
do
typedef struct sbool {bool b;} boolstruct;
and thenvector<boolstruct> bs;
std::vector< bool >
packs its contents so each Boolean value is stored in one bit, eight bits to a byte. This is memory-efficient but computationally intensive, since the processor must perform arithmetic to access the requested bit. And it doesn't work withbool
reference or pointer semantics, since bits within a byte do not have addresses in the C++ object model.You can still declare a variable of type
std::vector<bool>::reference
and use it as if it werebool&
. This allows generic algorithms to be compatible.In C++11, you can work around this using
auto
and the&&
specifier which automatically selects an lvalue reference bound to the vector element or an rvalue reference bound to a temporary.