Share via


__declspec

Microsoft Specific

**__declspec(extended-attribute)**declarator

extended-attribute
allocate(“segname”)
dllimport
dllexport
naked
noreturn
nothrow
novtable
property(
{get=get_func_name|,put=put_func_name})
selectany
thread
uuid(“ComObjectGUID”)

The extended attribute syntax for qualifying storage-class information simplifies and standardizes Microsoft-specific extensions to the C and C++ languages. Extended attribute syntax uses the __declspec keyword to qualify extended storage-class information. The __declspec keyword specifies that an instance of a given type is to be stored with a Microsoft-specific storage-class attribute.

Extended attribute syntax supports these Microsoft-specific storage-class attributes: allocate, thread, naked, dllimport, dllexport, nothrow, noreturn, novtable, and selectany. It also supports these COM-object attributes: property and uuid. These attributes are properties only of the declaration of the object or function to which they are applied; they do not redefine the type attributes of the object itself.

The __declspec keywords should be placed at the beginning of a simple declaration. THe compiler ignores, without warning, any __declspec keywords placed after * or & and in front of the variable identifier in a declaration.

A __declspec attribute specified in the beginning of a user-defined type declaration applies to the variable of that type. For example:

__declspec(dllimport) class X {} varX;

In this case, the attribute applies to varX. A __declspec attribute placed after the class or struct keyword applies to the user-defined type. For example:

class __declspec(dllimport) X {};

In this case, the attribute applies to X.

The general guideline for using the __declspec attribute for simple declarations is as follows:

decl-specifier-seqinit-declarator-list;

The decl-specifier-seq should contain, among other things, a base type (e.g. int, float, a typedef, or a class name), a storage class (e.g. static, extern), or the __declspec extension. The init-declarator-list should contain, among other things, the pointer part of declarations. For example:

__declspec(selectany) int * pi1 = 0;   //OK, 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

The following code declares an integer thread local variable and initializes it with a value:

// Example of the __declspec keyword
__declspec( thread ) int tls_i = 1;

END Microsoft Specific