In the simple code below for (auto& v : vb)
does not compile whereas for (auto & v : vi)
compiles fine.
The only difference is that vb
is a vector of bool
and vi
is a vector of int
.
The error message:
error C2440: 'initializing': cannot convert from 'std::_Vb_referencestd::_Wrap_alloc<std::allocator<std::_Vbase>>' to 'std::_Vb_referencestd::_Wrap_alloc<std::allocator<std::_Vbase>> &'`
is not really helpful for me.
#include <vector>
int main()
{
std::vector<bool> vb{ false, false };
for (auto & v : vb) //<<< error messages refers to this line
v = true;
std::vector<int> vi{ 1, 2 };
for (auto & v : vi) //<<< compiles fine
v = 2;
}
bool
being a builtin C++ type (basically a special kind of char
), both range based loops should compile.