__restrict

__declspec ( restrict ) 修饰符一样,__restrict 关键字(两个前导下划线“_”)指示符号在当前范围中未使用别名__restrict 关键字与 __declspec (restrict) 修饰符在下列方面不同:

  • __restrict 关键字仅对变量有效,而 __declspec (restrict) 仅对函数声明和函数定义有效。

  • 对于从 C99 开始的 C,__restrictrestrict 类似,并且可在 /std:c11/std:c17 模式下使用,但 __restrict 在 C++ 和 C 程序中均可使用。

  • 使用 __restrict 时,编译器将不会传播变量的非别名属性。 即,如果你向非 __restrict 变量分配 __restrict 变量,则编译器仍允许非 __restrict 变量使用别名。 这与 C99 C 语言 restrict 关键字的行为不同。

通常,如果想要影响整个函数的行为,请使用 __declspec (restrict) 而不是关键字。

为了与以前的版本兼容,除非指定了编译器选项 /Za(禁用语言扩展),否则 _restrict__restrict 的同义词。

在 Visual Studio 2015 及更高版本中,可以对 C++ 引用使用 __restrict

注意

在同时包含 volatile 关键字的变量上使用时,volatile 将优先。

示例

// __restrict_keyword.c
// compile with: /LD
// In the following function, declare a and b as disjoint arrays
// but do not have same assurance for c and d.
void sum2(int n, int * __restrict a, int * __restrict b,
          int * c, int * d) {
   int i;
   for (i = 0; i < n; i++) {
      a[i] = b[i] + c[i];
      c[i] = b[i] + d[i];
    }
}

// By marking union members as __restrict, tell compiler that
// only z.x or z.y will be accessed in any given scope.
union z {
   int * __restrict x;
   double * __restrict y;
};

另请参阅

关键字