Declaration Specifier (__declspec)

The __declspec keyword starts a declaration specifier. It is intended for use in a declaration, where it specifies so-called extended storage-class attributes for the object that is being declared.

Syntax

__declspec ( [[[,] attribute ]...] )

It is an error (C2054, with message text that is at best cryptic) if the first token after the __declspec is not a left parenthesis. There may then be any number of attributes, including none. Before, after, between or instead of attributes, there may be any number of commas, including none. The declaration specifier ends with the first right parenthesis that is not interpreted as belonging to an attribute.

Where an attribute is expected, it is an ordinary syntax error (C2059) if the first token is not an identifier and a more specific error (C2485) if the identifier is not recognised as introducing one of the supported attributes. The syntax requirements then vary with the attribute.

Extended Attributes

In the following list of extended attributes for the __declspec keyword in Microsoft Visual C++ version 13.00.9466, those that seem to be missing from the product documentation are highlighted yellow.

Of these, only align, allocate, implementation_key, property and uuid have non-trivial syntactic expectations. For all others, the attribute is just the one identifier.

Standard Order

There is no prescribed ordering of extended attributes, whether they are placed all in one __declspec or each in its own or in any arrangement in-between. The closest to a standard order, of sorts, is given by the compiler’s own practice when listing extended attributes

Not all of an object’s extended attributes get listed in these cases, but those that do are listed all in the one __declspec in the order:

each with a trailing space. Not only does the compiler not itself use commas when listing extended attributes, but the product documentation seems not to mention anywhere that commas are permitted.