__declspec(selectany)

From Appmethod Topics
Jump to: navigation, search

Go Up to Keywords, Alphabetical Listing Index


Category

Modifiers, Keyword Extensions, Storage Class Specifiers

Syntax

__declspec( selectany ) declarator

A global data item can normally be initialized only once in an application or library. This attribute can be used in initializing global data defined by headers, when the same header appears in more than one source file.

Note: This attribute can only be applied to the actual initialization of global data items that are externally visible.

The following code shows how to use the selectany attribute:

//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 __declspecselectany) int x4=4;

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

See also