V2639. MISRA. Default association should appear as either the first or the last association of a generic selection.
This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.
This diagnostic rule is relevant only for C.
Default association declared with the default
keyword should be placed either first or last in the _Generic
selection (C11). This structure enhances code readability for developers.
The example:
#define abs(Y)( _Generic( (Y) \
, long : labs \
, default : abs \
, long long: llabs)(Y))
long foo(long x)
{
return abs(x);
}
The default
association is placed between int
and char
. This ordering of the association list complicates the _Generic
construction.
The fixed code:
// First option
#define abs(Y)( _Generic( (Y) \
, default : abs \
, long : labs \
, long long: llabs)(Y))
// Second option
#define abs(Y)( _Generic( (Y) \
, long : labs \
, long long: llabs \
, default : abs)(Y))
long foo(long x)
{
return abs(x);
}
This diagnostic is classified as:
|