Share via


selectany

Microsoft 固有の仕様 →

宣言されたグローバル データ項目 (変数やオブジェクト) を選択してパッケージ化された関数 (COMDAT) であることをコンパイラに指示します。

__declspec( selectany ) declarator

解説

リンク時その結果重複定義発生した場合リンカーの選択して 1 および破棄されます。そのほかリンカー オプション /OPT: REF 最適化 ()は選択されていますが結果が発生する場合リンカー出力のすべての参照を削除するデータ項目を削除。

宣言のグローバル静的関数またはメソッドにコンストラクターと代入は参照を作成して/OPT を防ぐ : REF を削除する。このようなコードの副作用は他のデータへの参照がない場合に依存しないでください。

動的にグローバル オブジェクトの初期化はselectany 参照はオブジェクトの初期化コードも破棄されます。

グローバル データ項目は一度だけ EXE または DLL プロジェクトでは通常初期化できます。selectany はヘッダーで定義されているグローバル データの初期化で同じヘッダーを複数のソース ファイルで表示するときに使用できます。selectany は C および C++ のコンパイラの両方で使用できます。

[!メモ]

selectany外部から参照できるグローバル データ項目の実際の初期化にのみ適用できます。

使用例

このコードは selectany 属性の使用方法を示します :

//Correct - x1 is initialized and externally visible 
__declspec(selectany) int x1=1;

//Incorrect - const is by default static in C++, so 
//x2 is not visible externally (This is OK in C, since
//const is not by default static in C)
const __declspec(selectany) int x2 =2;

//Correct - x3 is extern const, so externally visible
extern const __declspec(selectany) int x3=3;

//Correct - x4 is extern const, so it is externally visible
extern const int x4;
const __declspec(selectany) int x4=4;

//Incorrect - __declspec(selectany) is applied to the uninitialized
//declaration of x5
extern __declspec(selectany) int x5;

// OK: dynamic initialization of global object
class X {
public:
X(int i){i++;};
int i;
};

__declspec(selectany) X x(1);

このコードは/OPT: ICF のリンカー オプションを使用してデータの折りたたみ結果を確認するために selectany 属性の使用方法を示します。データが selectany でマークされ 定数 (読み取り専用) セクションに配置する必要があることに注意してください。明示的に読み取り専用セクションを指定する必要があります。

// selectany2.cpp
// in the following lines, const marks the variables as read only
__declspec(selectany) extern const int ix = 5;
__declspec(selectany) extern const int jx = 5;
int main() {
   int ij;
   ij = ix + jx;
}

終了 Microsoft 固有の仕様→

参照

関連項目

__declspec

C++ のキーワード