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.

Comments on How to write a macro that discards the const qualifier, for any type?

Post

How to write a macro that discards the const qualifier, for any type?

+3
−0

How to write a macro that discards the const qualifier, for any type?

I hope some combination of typeof and a cast will do, but haven't found the combination.

I tried this, without luck:

#define discard_const(_x)  ((typeof(_x + 0)) (_x))

struct t {
    char *s;
};

char *
foo(const struct t *r)
{
    return discard_const(r->s);
}

I also tried: #define discard_const(_x) ({__auto_type _y = (_x); _y;}), but not luck

The objective is that if I cast to (char *), I fear that if the type of s changes in the future, the cast might silence any warnings, so I want the type to be automatically calculated, and I only want to discard const.

Of course, with pragmas I may disable any const warnings for that specific line, but a macro that does it within the language would be nicer.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

Most of the time, you don't (3 comments)
The variable you're applying `discard_const` to is not const. (4 comments)
Most of the time, you don't
Lundin‭ wrote over 2 years ago

Most of the time, you don't want to discard const qualifiers since that invokes undefined behavior in many cases. The appropriate solution is either to do a copy of the value or to reconsider your design.

alx‭ wrote over 2 years ago

Agree. I don't even remember why that solution came through my mind. I didn't ask to close the question, just for curiosity of the answers :)

Lundin‭ wrote over 2 years ago

alx‭ There are various tricks involving union type punning where the union members are pointers of different qualifiers. But that mostly relies on the C rules of effective type being underspecified in regards of type qualifiers, so I'm not sure if anyone could tell if it would be well-defined or not. If the lvalue of the original variable is not const-qualified, then arguably one should be able to just cast.