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?
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.
2 comment threads