Recently version 16.9.5 of Visual Studio 2019 has been released. It apparently introduced new warning:
[[nodiscard]] __declspec(dllexport) bool foo(); //ok
__declspec(dllexport) [[nodiscard]] bool bar(); // warning C5240: 'nodiscard': attribute is ignored in this syntactic position
Actually I thought that both nodiscard and dllexport are attributes that can appear in any order, or it is not?
I got this warning today too, so decided to look into it. This requires looking a bit at the standard, and putting different sections together.
According to
[dcl.fct.def.general]
, a function is defined as:The reason this is important, is that it specifies that the optional
decl-specifier-seq
comes after theattribute-specifier-seq
(which includes[[nodiscard]]
).Now, according to
[dcl.spec.general]
, adecl-specifier-seq
is defined as:According to Microsoft's documentation of
__declspec
, it is defined as adecl-specifier
; thus, the__declspec(dllexport)
must come after the[[nodiscard]]
attribute.Note:
Thus, the
[[nodiscard]]
after the__declspec(dllexport)
applies only to the__declspec(dllexport)