ddemidov / vexcl

VexCL is a C++ vector expression template library for OpenCL/CUDA/OpenMP
http://vexcl.readthedocs.org
MIT License
702 stars 82 forks source link

Printing vex::vector<bool> #197

Closed atoader closed 8 years ago

atoader commented 8 years ago

First of all, great work with VexCL! I have been playing with VexCL and I noticed that the following code does not compile:

const size_t n = 5;
bool* data = new bool[n];
for(size_t i=0; i<n; ++i){
        data[i] = false;
}

vex::vector<bool> A(ctx, n, data);
std::cout<<A;

The compiler prints the following error message:

In file included from ./vexcl/vexcl/vexcl.hpp:39:0, from main.cc:4: ./vexcl/vexcl/vector.hpp: In instantiation of ‘void vex::copy(const vex::vector&, std::vector<_RealType>&, bool) [with T = bool]’: ./vexcl/vexcl/vector.hpp:992:17: required from ‘std::ostream& vex::operator<<(std::ostream&, const vex::vector&) [with T = bool; std::ostream = std::basic_ostream]’ main.cc:29:14: required from here ./vexcl/vexcl/vector.hpp:874:5: error: invalid use of void expression dv.read_data(0, dv.size(), hv.data(), blocking);

From what I understand, the overload of operator<< for vex::vector uses a std::vector internally and it uses the data() member of the std::vector to transfer the data. For std::vector<bool> the data() member does not exist.

I think that the problem could be fixed by providing a specialization of the operator<< for bool. Let me know what you think. I can look into the code and patch it if you think this should be fixed.

ddemidov commented 8 years ago

Could you just replace vector<bool> with vector<char>?. bool and char are binary compatible, but std::vector<char> is a real vector, whereas std::vector<bool> is really not.

In case it is important for you to really work with bools, then providing a specification for operator<< should be easy. It is implemented here.