Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on How to use _Generic on restrict pointers like C23 tells me to do?
Post
How to use _Generic on restrict pointers like C23 tells me to do?
I just noticed that ISO 9899:2024 6.7.4 has this weird text added, which wasn't there previously:
The intended use of the
restrictqualifier (like theregisterstorage class) is to promote optimization, and deleting all instances of the qualifier from all preprocessing translation units composing a conforming program does not change its meaning (i.e. observable behavior), unless_Genericis used to distinguish whether or not a type has that qualifier.
Ok... But how exactly am I to use _Generic to do that? Notably, pointed-at data cannot be restrict qualified, only the pointer object itself.
C11 had an ambiguous wording where it wasn't clear if the first controlling expression in the _Generic association list would respect qualifiers or not, resulting in different compilers behaving differently.
C17 fixed this by adding this text to 6.5.1.1:
The type of the controlling expression is the type of the expression as if it had undergone an lvalue conversion
Lvalue conversion meaning that qualifiers are stripped. So for any type* restrict, lvalue conversion will always turn it into type*.
Example to illustrate:
#include <stdio.h>
int main()
{
char* restrict ptr =
_Generic(ptr,
char*: "I'm a char*.",
char* restrict: "Good luck getting this printed");
puts(ptr);
}
Output on any C17/C23 compliant compiler:
I'm a char*.
clang helpfully gives extra diagnosis:
warning: due to lvalue conversion of the controlling expression, association of type 'char *restrict' will never be selected because it is qualified [-Wunreachable-code-generic-assoc]
I'm am missing something here or should this be posted as a Defect Report for C23?

1 comment thread