Generalize const_cast(), and use it more.#1374
Open
alejandro-colomar wants to merge 3 commits intoshadow-maint:masterfrom
Open
Generalize const_cast(), and use it more.#1374alejandro-colomar wants to merge 3 commits intoshadow-maint:masterfrom
alejandro-colomar wants to merge 3 commits intoshadow-maint:masterfrom
Conversation
a6f929b to
73cf573
Compare
b8464aa to
aa569b9
Compare
7ab1ed9 to
c3b4fd7
Compare
d0ca9b9 to
a9f17dd
Compare
7c86adf to
64085c6
Compare
e92c7e6 to
226fc05
Compare
226fc05 to
b55a55a
Compare
Collaborator
Author
|
Cc: @kees |
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?
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>
4f99453 to
e37687a
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