MISRA C:2012 Rule 23.4
Description
Rule Definition
A generic association shall list an appropriate type.
This rule comes from MISRA C™: 2012 Amendment 3.
Rationale
The controlling expression of a generic selection undergoes lvalue conversion before its type is compared to the type names in the association list. This lvalue conversion:
Removes top-level qualifications such as
const
,volatile
, oratomic
Converts functions and arrays to pointers
If your association list contains qualified types, arrays, or function types, then the controlling expression cannot match the association list. Consider this code:
typedef const uint32_t const_int; #define get_type_id_of(X) _Generic((X), const_int : 0, int : 1) const_int x = 7; get_type_id_of(x);
get_type_id_of(x)
evaluates to 1
because after lvalue conversion, the type of
x
matches with int
. To avoid this behavior, use
appropriate types in the association list.Using an unnamed struct
or union
in the
association list violates this rule because every unnamed struct
or
union
is a distinct type. An unnamed struct
in
the controlling expression does not match with an unnamed struct
in
the association list. Rather, the unnamed struct
in the controlling
expression matches the default association.
Polyspace Implementation
The rule checker reports a violation if the association list for a generic selection contains any of these:
A
const
qualified typeA
volatile
qualified typeAn
atomic
typeAn array or function type
An unnamed type
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Generic Selections |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2024a