Microsoft 特定
指定儲存類別資訊的擴充屬性語法會使用 __declspec 關鍵詞,指定指定類型的實例要與下面所列的Microsoft特定儲存類別屬性一起儲存。 其他儲存類別修飾詞的範例包括 static 和 extern 關鍵詞。 不過,這些關鍵詞是 C 和 C++ 語言 ANSI 規格的一部分,因此不會涵蓋擴充屬性語法。 擴充屬性語法可以簡化和標準化 C 和 C++ 語言中的 Microsoft 特定擴充功能。
語法
decl-specifier:
__declspec (
extended-decl-modifier-seq
)
extended-decl-modifier-seq:
extended-decl-modifier
選擇
extended-decl-modifier
extended-decl-modifier-seq
extended-decl-modifier:
align(
號碼)
allocate("
segname")
allocator
appdomain
code_seg("
segname")
deprecated
dllimport
dllexport
empty_bases
hybrid_patchable
jitintrinsic
naked
noalias
noinline
noreturn
nothrow
novtable
no_sanitize_address
process
property( { get=get-func-name | ,put=put-func-name } )
restrict
safebuffers
selectany
spectre(nomitigation)
thread
uuid("
ComObjectGUID")
空格符會分隔宣告修飾詞序列。 範例會出現在稍後的章節中。
擴充屬性文法支持這些 Microsoft 特定的儲存類別屬性:align、allocate、allocator、appdomain、code_seg、deprecated、dllexport、dllimport、empty_bases、jitintrinsic、naked、noalias、noinline、noreturn、nothrow、novtable、no_sanitize_address、process、restrict、safebuffers、selectany、spectre和 thread。 它也支援這些 COM 物件屬性:property 與 uuid。
code_seg、dllexport、dllimport、empty_bases、naked、noalias、nothrow、no_sanitize_address、property、restrict、selectany、thread和 uuid 儲存類別屬性只是套用物件或函式宣告的屬性。
thread 屬性只會影響數據和物件。
naked 和 spectre 屬性只會影響函式。
dllimport 和 dllexport 屬性會影響函式、數據和物件。
property、selectany和 uuid 屬性會影響 COM 物件。
為了與舊版相容,除非指定編譯程式選項 _declspec,否則 __declspec 與 /Za 同義。
__declspec 關鍵詞應該放在簡單宣告的開頭。 編譯程式會在宣告中,對於放置在 * 或 & 之後並放在變數標識符前的任何 __declspec 關鍵詞,予以忽略而不發出警告。
使用者定義型別宣告開頭指定的 __declspec 屬性會套用至該類型的變數。 例如:
__declspec(dllimport) class X {} varX;
在這裡情況下,屬性會套用至 varX。 放置在 __declspec 或 class 關鍵詞之後的 struct 屬性會套用至使用者定義型別。 例如:
class __declspec(dllimport) X {};
在這裡情況下,屬性會套用至 X。
針對簡單宣告使用 __declspec 屬性的一般指導方針如下:
decl-specifier-seq
init-declarator-list
;
decl-specifier-seq 應該包含基底類型(例如,int、float、typedef或類別名稱)、記憶體類別(例如,static、extern),或 __declspec 延伸模組。
init-declarator-list 應該包含宣告中指標部分等其他成分。 例如:
__declspec(selectany) int * pi1 = 0; //Recommended, selectany & int both part of decl-specifier
int __declspec(selectany) * pi2 = 0; //OK, selectany & int both part of decl-specifier
int * __declspec(selectany) pi3 = 0; //ERROR, selectany is not part of a declarator
下列程式代碼會宣告整數線程局部變數,並使用 值初始化它:
// Example of the __declspec keyword
__declspec( thread ) int tls_i = 1;
END Microsoft 特定設定