__restrict

__declspec 如同 ( restrict 修飾詞, __restrict 關鍵字 (兩個前置底線 '_') 表示符號在目前範圍中沒有別名。 關鍵字 __restrict__declspec (restrict) 修飾詞有下列不同之處:

  • 關鍵字 __restrict 只在變數上有效,而且 __declspec (restrict) 只在函式宣告和定義上有效。

  • __restrict類似于 restrict 從 C99 開始的 C,而且 /std:c11 可在 或 /std:c17 模式中使用,但 __restrict 可用於 C++ 和 C 程式。

  • 使用 時 __restrict ,編譯器不會傳播變數的 no-alias 屬性。 也就是說,如果您將變數指派 __restrict 給非 __restrict 變數,編譯器仍會允許非__restrict變數成為別名。 這與 C99 C 語言 restrict 關鍵字的行為不同。

一般而言,如果您想要影響整個函式的行為,請使用 __declspec (restrict) 而非 關鍵字。

為了與舊版相容, _restrict 除非指定編譯器選項 /Za (停用語言延伸模組), 否則 是 的 __restrict 同義字。

在 Visual Studio 2015 和更新版本中, __restrict 可用於 C++ 參考。

注意

在具有 關鍵字的 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;
};

另請參閱

關鍵字