I'm starting to add [[nodiscard]]
, [[noreturn]]
and [[fallthrough]]
to my code base, but I still worry the code might need to be used on an older C version.
I'm only worried about g , clang, and Visual Studio.
To be safe should I make every single usage depend on a version check for the exact version it appeared? Or, do these compilers accept (or does the spec even mandate they accept) any [[
attribute]]
even if unknown, starting with C 11?
Were these allowed and ignored (or supported) even before C 11? (I ask not what the spec says but what the software actually did.)
#if __cplusplus >= 201703L
#define MyNamespace_NoReturnCPP17 [[noreturn]]
#else
#define MyNamespace_NoReturnCPP17
#endif
(Off subject but if I were on the C committee, I'd re-use the case
keyword to do the same thing as switch
, when followed by (
, except it would in effect have an automatic break
before each case
label. And within the case
's block (and nowhere else), fallthrough
would be accepted as a keyword, that suppresses that automatic break
.)
I note the C 14 spec 7.6.1.5 says behavior is "implemenation-defined" but I'm asking a more practical question about what compilers actually did. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf
The C 17 spec 10.6.1.6 says "Any attribute-token that is not recognized by the implementation is ignored." That answers my question for C 17 and newer, but again my question is about earlier compilers. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf
CodePudding user response:
To be safe should I make every single usage depend on a version check for the exact version it appeared? Or, do these compilers accept (or does the spec even mandate they accept) any [[attribute]] even if unknown, starting with C 11?
I prefer adding version checks for safety although mainstream compilers will throw a warning for unknown attributes.
- gcc
-Wno-ignored-attributes
/-Wattributes
- Clang
-Wunknown-attributes
- MSVC
C5030
I'm not sure these warning options(and corresponding behavior) have been added since the first C 11 feature-complete version, so you'd better do some checks.
Were these allowed and ignored (or supported) even before C 11?
Since standard attributes syntax is supported starting from C 11, compilers will have trouble at the parsing stage. Demo
CodePudding user response:
use alternatives like GNU and IBM language extensions attribute((...)), Microsoft extension __declspec(), etc.