__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 是 的同義字。
在 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;
};