Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

How to use _Generic on restrict pointers like C23 tells me to do?

+4
−0

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 restrict qualifier (like the register storage 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 _Generic is 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?

History

1 comment thread

pointees can be restrict too (8 comments)

Sign up to answer this question »