devoured elysium
devoured elysium

Reputation: 105027

Difference between _declspec and __declspec?

I sometimes see keywords starting with two underscores and other times just one. Is there any difference?

Upvotes: 10

Views: 3125

Answers (2)

AlexF
AlexF

Reputation: 427

The keyword with two underscores __declspec should be used. While Microsoft compiler accepts the keyword with one underscore _declspec, it is only for compatibility reasons with the old compiler versions. It is not accepted by MSYS2 GCC. Therefore, using it would cause issues with GCC. Microsoft's documentation states the following:

For compatibility with previous versions, _declspec is a synonym for __declspec unless compiler option /Za (Disable language extensions) is specified.

Upvotes: 0

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99535

I believe that _declspec is older name of the same Microsoft specific keyword __declspec. From a C++ Standard point of view, two underscores are more correct than a single underscore for an extension like this. That's according to 17.4.3.1.2/1:

Certain sets of names and function signatures are always reserved to the implementation:

  • Each name that contains a double underscore (_ _) or begins with an underscore followed by an upper- case letter (2.11) is reserved to the implementation for any use.
  • Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

Upvotes: 17

Related Questions