2017-07-06 48 views
0

以下C++的OpenCL代碼編譯細跟克++ -c no_x.cpp:OpenCL的矢量類型:無法訪問聯合在一起分量x,y和z與C++ 11啓用

// 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嗎?

+0

xy.s [0]是最便攜的方式,它很醜,但工作。 – DarkZeros

回答

1

在OpenCL的標題(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),您將無法訪問這些成員。

+0

嗯,我不知道我的opencl版本比你看到的版本更新還是更新,但是我的版本是由__CL_HAS_ANON_STRUCT__控制的,而且我在cl_platform.h中看到,這反過來取決於STRICT_ANSI,我猜-std = C++ 11定義它。 – RubeRad

+0

@ user2387508'-std = C++ 11'對其進行了定義,但您可以在g ++中簡單使用參數'-U__STRICT_ANSI__'。 – Lovy

+0

哦,甜蜜!這是訣竅,謝謝! – RubeRad