OpenCL向量类型:Can';t使用C+访问联合组件x、y、z+;11启用 下面的C++ OpenCL代码用G++C NONYX.CPP:编译成精细代码 // no_x.cpp #include <CL/cl.h> void func() { cl_double2 xy; xy.x = 1.0; xy.y = 2.0; }

OpenCL向量类型:Can';t使用C+访问联合组件x、y、z+;11启用 下面的C++ OpenCL代码用G++C NONYX.CPP:编译成精细代码 // no_x.cpp #include <CL/cl.h> void func() { cl_double2 xy; xy.x = 1.0; xy.y = 2.0; },c++,c++11,opencl,unions,C++,C++11,Opencl,Unions,我可以用xy.s[0]、xy.s[1]等来解决这个问题,但这很难看(这就是OpenCL提供.x、.y组件的原因)。C++11是怎么造成这种情况的?我通常可以不使用C++11编译OpenCL吗 在OpenCL标题(cl.h中包含的cl_platform.h)中,cl_double2的定义如下: typedef union { cl_double CL_ALIGNED(16) s[2]; #if defined( __GNUC__) && ! defined( __STRI

我可以用xy.s[0]、xy.s[1]等来解决这个问题,但这很难看(这就是OpenCL提供.x、.y组件的原因)。C++11是怎么造成这种情况的?我通常可以不使用C++11编译OpenCL吗

在OpenCL标题(cl.h中包含的cl_platform.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预处理器,或者定义了
\u STRICT\u ANSI\u
,您将无法访问这些成员。

hmmm,我不知道我的opencl版本是否比您看到的版本旧或新,但我的版本由CL\u控制,并且没有一个结构,我在前面的cl_platform.h中看到,这反过来又取决于严格的ANSI,我猜-std=c++11定义了它。@user2387508
-std=c++11
定义了它,但是你可以简单地在g++xy.s[0]中使用参数
-U_严格的ANSI
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;