OpenCL 矢量类型:在启用 C++11 的情况下无法访问联合组件 x、y、z
OpenCL vector types: Can't access unioned components x,y,z with C++11 enabled
以下 C++ OpenCL 代码可以使用 g++ -c no_x.cpp:
正常编译
// no_x.cpp
#include <CL/cl.h>
void func() {
cl_double2 xy;
xy.x = 1.0;
xy.y = 2.0;
}
但是在启用 C++-11 的情况下,同一文件会出现错误:
$ g++ -std=c++11 -c no_x.cpp
nox.cpp: In function ‘void func()’:
nox.cpp:7:7: error: ‘union cl_double2’ has no member named ‘x’
xy.x = 1.0;
^
nox.cpp:8:7: error: ‘union cl_double2’ has no member named ‘y’
xy.y = 2.0;
^
我可以用 xy.s[0]、xy.s[1] 等绕过它,但这很丑陋(这肯定是 OpenCL 提供 .x、.y 组件的原因)。导致这种情况的 C++11 怎么了?我一般不能用 C++11 编译 OpenCL 吗?
在 OpenCL headers(cl_platform.h,包含在 cl.h 中),cl_double2
定义如下:
typedef union
{
cl_double CL_ALIGNED(16) s[2];
#if defined( __GNUC__) && ! defined( __STRICT_ANSI__ )
__extension__ struct{ cl_double x, y; };
__extension__ struct{ cl_double s0, s1; };
__extension__ struct{ cl_double lo, hi; };
#endif
#if defined( __CL_DOUBLE2__)
__cl_double2 v2;
#endif
}cl_double2;
因此,如果您的编译器不使用 GNU 预处理器,或者如果定义了 __STRICT_ANSI__
(g++ may define it),您将无法访问这些成员。
以下 C++ OpenCL 代码可以使用 g++ -c no_x.cpp:
正常编译// no_x.cpp
#include <CL/cl.h>
void func() {
cl_double2 xy;
xy.x = 1.0;
xy.y = 2.0;
}
但是在启用 C++-11 的情况下,同一文件会出现错误:
$ g++ -std=c++11 -c no_x.cpp
nox.cpp: In function ‘void func()’:
nox.cpp:7:7: error: ‘union cl_double2’ has no member named ‘x’
xy.x = 1.0;
^
nox.cpp:8:7: error: ‘union cl_double2’ has no member named ‘y’
xy.y = 2.0;
^
我可以用 xy.s[0]、xy.s[1] 等绕过它,但这很丑陋(这肯定是 OpenCL 提供 .x、.y 组件的原因)。导致这种情况的 C++11 怎么了?我一般不能用 C++11 编译 OpenCL 吗?
在 OpenCL headers(cl_platform.h,包含在 cl.h 中),cl_double2
定义如下:
typedef union
{
cl_double CL_ALIGNED(16) s[2];
#if defined( __GNUC__) && ! defined( __STRICT_ANSI__ )
__extension__ struct{ cl_double x, y; };
__extension__ struct{ cl_double s0, s1; };
__extension__ struct{ cl_double lo, hi; };
#endif
#if defined( __CL_DOUBLE2__)
__cl_double2 v2;
#endif
}cl_double2;
因此,如果您的编译器不使用 GNU 预处理器,或者如果定义了 __STRICT_ANSI__
(g++ may define it),您将无法访问这些成员。