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
opt
extended-decl-modifier
extended-decl-modifier-seq
extended-decl-modifier
:
align(
number)
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
) をサポートしています。 また、property
と uuid
の COM オブジェクト属性もサポートしています。
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 オブジェクトに影響します。
以前のバージョンとの互換性のために、コンパイラ オプション /Za
(言語拡張機能の無効化) が指定されていない限り、_declspec
は __declspec
のシノニムです。
__declspec
キーワードは、単純な宣言の先頭に配置する必要があります。 コンパイラは、 * または & の後に配置され、宣言内の変数識別子の前に配置された __declspec
キーワードを、警告なしで無視します。
ユーザー定義型宣言の先頭で指定された __declspec
属性は、その型の変数に適用されます。 例えば:
__declspec(dllimport) class X {} varX;
この場合、属性は varX
に適用されます。 class
キーワードまたは struct
キーワードの後に配置された __declspec
属性は、ユーザー定義型に適用されます。 例えば:
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;
Microsoft 固有の仕様はここまで