Can the vector types be made more C++ friendly?

The OpenCL vector types defined in cl_platform.h do not play well with C++. You can not do something this simple:

#include <vector>
typedef float cl_float4[4] attribute((aligned(16)));
int main()
std::vector<cl_float4> vals;

cl_float4 a = {1.0f, 2.0f, 3.0f, 4.0f};

Because “ISO C++ forbids assignment of arrays”. If the definition would be changed to a struct like the following:
typedef struct cl_float4
float x,y,z,w;
} cl_float4 attribute((aligned(16)));

Not only would this play nicer with C++, it would also mirror the behaviour of float4 in device code, which is a win-win in my book.


See also: … 01707c9cc7

It is worth noting that cl_platform.h is only an example and a particular vendor is free to implement this file as they feel necessary. That said we have also run into this issue but after consideration we feel it makes more sense to implement the host vector types using the compiler’s extension for vector types.

For example cl_float4 with Visual Studio 2008 could be implemented as:

typedef union __declspec(intrin_type) _CRT_ALIGN(16) cl_float4
cl_float f32[4];
} cl_float4;

and with GNUC defined, this is what we have:

typedef cl_float cl_float4 attribute ((vector_size (16), may_alias));

As in both cases these define a non-array type it is valid to do assignment and so your example works.