Generalize const_cast(), and use it more. - #1374
Open
alejandro-colomar wants to merge 3 commits into
Open
Conversation
alejandro-colomar
force-pushed
the
const_cast
branch
2 times, most recently
from
October 19, 2025 08:24
a6f929b to
73cf573
Compare
alejandro-colomar
force-pushed
the
const_cast
branch
3 times, most recently
from
November 1, 2025 19:56
b8464aa to
aa569b9
Compare
alejandro-colomar
force-pushed
the
const_cast
branch
6 times, most recently
from
November 10, 2025 12:14
7ab1ed9 to
c3b4fd7
Compare
alejandro-colomar
force-pushed
the
const_cast
branch
3 times, most recently
from
November 19, 2025 21:37
d0ca9b9 to
a9f17dd
Compare
alejandro-colomar
force-pushed
the
const_cast
branch
from
November 28, 2025 12:28
7c86adf to
64085c6
Compare
alejandro-colomar
marked this pull request as ready for review
November 28, 2025 12:30
alejandro-colomar
force-pushed
the
const_cast
branch
2 times, most recently
from
December 7, 2025 14:07
e92c7e6 to
226fc05
Compare
alejandro-colomar
force-pushed
the
const_cast
branch
from
December 26, 2025 12:20
226fc05 to
b55a55a
Compare
Collaborator
Author
|
Cc: @kees |
alejandro-colomar
force-pushed
the
const_cast
branch
2 times, most recently
from
February 26, 2026 16:46
95240c2 to
4f99453
Compare
kees
reviewed
Feb 26, 2026
|
|
||
|
|
||
| #define const_cast(T, p) _Generic(p, const T: (T) (p)) | ||
| #define const_cast(T, p) _Generic(p, const T: (T) (p), default: (p)) |
There was a problem hiding this comment.
A down-side of this change is that if T is ever already const-qualified, this code will fail to build (on at least Clang), as const const ... is not a valid type qualifier. The original _Generic() macros handled this by matching const with a pass-thru, and only adding const when it was missing.
Collaborator
Author
There was a problem hiding this comment.
$ git log --pretty=reference lib/cast.h
eb71706b (*/: Fix including <config.h> as system header, 2025-07-15)
69f74dbf (lib/cast.h: const_cast(): Reimplement with _Generic(3), 2024-05-15)
e9fc8fc7 (lib/cast.h: const_cast(): Add macro for dropping 'const', 2024-01-07)By "the original _Generic() macros" you mean this, right?
$ git show 69f74dbf:lib/cast.h | grepc -h const_cast
#define const_cast(T, p) _Generic(p, const T: (T) (p))69f74db (2024-06-04; "lib/cast.h: const_cast(): Reimplement with _Generic(3)")
I think that would also fail for const char *, as it would expand const T as const const char *, right?
alejandro-colomar
force-pushed
the
const_cast
branch
from
March 4, 2026 18:43
4f99453 to
e37687a
Compare
alejandro-colomar
force-pushed
the
const_cast
branch
from
April 12, 2026 08:57
e37687a to
396b3b1
Compare
It won't hurt; if the type is convertible, it will be accepted; and if it's not convertible, the compiler will reject it at call site. This 'default' branch allows converting a QChar** into a char**. Signed-off-by: Alejandro Colomar <alx@kernel.org>
This allows grepping for casts more easily. It also reduces the damage of a bogus cast. Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar
force-pushed
the
const_cast
branch
from
July 20, 2026 20:47
396b3b1 to
aa4a07b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Revisions:
v1b
v1c
v1d
v1e
v1f
v1g
v1h
v1i