I am dealing with a library of which I cannot change the data types. I need to call a function that takes a bool
array. The code that I am working with uses std::vector<bool>
to store the bools. I have read a lot about std::vector<bool>
and the associated problems on SO, but I have not found the most elegant solution for my problem. This is a code where performance is crucial. How do I deal with this problem in the most efficient way? I can change the type stored in the std::vector
, but I cannot escape from using std::vector
.
In my own problem, I have to call Fortran functions, but I have made a minimal example in C++ that illustrates the problem.
#include <cstdio>
#include <vector>
void print_c_bool(bool* bool_array, size_t n)
{
for (int i=0; i<n; ++i)
printf("%d, %d\n", i, bool_array[i]);
}
int main()
{
// This works.
bool bool_array[5] = {true, false, false, true, false};
print_c_bool(bool_array, 5);
// This is impossible, but how to solve it?
std::vector<bool> bool_vector {true, false, false, true, false};
// print_c_bool(bool_vector.data(), bool_vector.size());
return 0;
}