MISRA C:2012 Rule 23.4
Description
Rule Definition
A generic association shall list an appropriate type1 .
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, or- atomic
- 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 - constqualified type
- A - volatilequalified type
- An - atomictype
- An 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
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace® Bug Finder™ documentation are from the following MISRA standards:
- MISRA C:2004 
- MISRA C:2012 
- MISRA C:2023 
- MISRA C++:2008 
- MISRA C++:2023 
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.