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)
The variable you're applying `discard_const` to is not const.
celtschk‭ wrote over 2 years ago

Note that const struct t* is not a const type, but a non-const pointer to a const type. Therefore there's no top-level const to discard. You can easily see the non-constness by adding the statement r = 0; to the function. If r were of a const type, that wouldn't compile.

alx‭ wrote over 2 years ago

But I'm discarding it from r->s, which is const char *.

celtschk‭ wrote over 2 years ago · edited over 2 years ago

Sorry, I misread. But then, r->s is not const char*. It is char* const. And while it does have a top-level const, that doesn't matter because you are just returning the value anyway.

Indeed, your code compiles just fine. And it does so even if you remove discard_const completely, changing the return statement just to return r->s;. I didn't even get a warning with -Wall -Wextra. Try it online!

Anyway, it's not really clear to me what exactly you want to achieve.

alx‭ wrote over 2 years ago

Ahh, you're right. Just some confusion of mine then. For some reason I thought I needed to discard the const in some code, but I don't need to.