在某些 C++ 编译器上使用 restrict 限定符的编译器错误
Compiler error using restrict qualifier on certain C++ compilers
我有一个函数,它以 double
的指针作为参数,并被限定为 restrict
。请注意,Intel 编译器使用 restrict
,但在 GCC 的情况下,我们将限定符替换为 __restrict__
。
void Stats::calc_flux_2nd(double* restrict data,
double* restrict datamean,
double* restrict w)
{
// ...
// Set a pointer to the field that contains w,
// either interpolated or the original
double* restrict calcw = w;
// ...
此代码使用 GCC 或 Clang 编译没有任何问题,但 IBM BlueGene 编译器给出以下错误:
(W) Incorrect assignment of a restrict qualified pointer.
Only outer-to-inner scope assignments between restrict pointers are
allowed. This may result in incorrect program behavior.
我不明白如何解释这个错误,因为我没有更改变量的签名,我也不知道我是在引入未定义的行为还是 IBM BlueGene 编译器有误。
IBM 的 XL C/C++ 编译器不支持您的构造,在他们的 documentation 中也有说明。您不能将受限指针分配给彼此。您可以通过创建一个新的块作用域和一组新的指针来解决这个问题。
{
int * restrict x;
int * restrict y;
x = y; /* undefined */
{
int * restrict x1 = x; /* okay */
int * restrict y1 = y; /* okay */
x = y1; /* undefined */
}
}
我有一个函数,它以 double
的指针作为参数,并被限定为 restrict
。请注意,Intel 编译器使用 restrict
,但在 GCC 的情况下,我们将限定符替换为 __restrict__
。
void Stats::calc_flux_2nd(double* restrict data,
double* restrict datamean,
double* restrict w)
{
// ...
// Set a pointer to the field that contains w,
// either interpolated or the original
double* restrict calcw = w;
// ...
此代码使用 GCC 或 Clang 编译没有任何问题,但 IBM BlueGene 编译器给出以下错误:
(W) Incorrect assignment of a restrict qualified pointer.
Only outer-to-inner scope assignments between restrict pointers are
allowed. This may result in incorrect program behavior.
我不明白如何解释这个错误,因为我没有更改变量的签名,我也不知道我是在引入未定义的行为还是 IBM BlueGene 编译器有误。
IBM 的 XL C/C++ 编译器不支持您的构造,在他们的 documentation 中也有说明。您不能将受限指针分配给彼此。您可以通过创建一个新的块作用域和一组新的指针来解决这个问题。
{
int * restrict x;
int * restrict y;
x = y; /* undefined */
{
int * restrict x1 = x; /* okay */
int * restrict y1 = y; /* okay */
x = y1; /* undefined */
}
}