r/cpp • u/antiquark2 #define private public • Feb 24 '26
P4019R0: constant_assert (Jonas Persson)
https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2026/p4019r0.pdf
21
Upvotes
r/cpp • u/antiquark2 #define private public • Feb 24 '26
23
u/Nolia_X Feb 24 '26
Please stop forgetting about custom error message
10
u/jdehesa Feb 24 '26
So it makes your code less portable? How would I use it in practice? I find it hard to imagine useful applications beyond studying the capabilities of different compilers.
5
u/Nicksaurus Feb 24 '26
I was literally just looking for something like this the other day. I have a parser for binary messages where it's an error to read the fields of the message out of order, which is enforced with runtime checks. The information used in these checks is all available at compile time and they can reliably be optimised away, but it would be very cumbersome to enforce it with the type system
I tried GCC's
__builtin_constant_p, but it didn't seem to be able to identify when the checks were optimised outAlso, besides producing errors/warnings, I think it would be very useful to hint to the optimiser that it has a target to aim for
7
u/ravixp Feb 24 '26
There is a really useful insight here. The analysis that goes into the optimizer (control flow analysis, escape analysis, reasoning over the range of possible values for a variable, etc) would be really useful for static analysis of program correctness. The mechanism here, where it’s exposed as a new kind of assert, doesn’t seem workable. But there is potential here.
A lot of what you’d want to do with this is already available through a tool like clang-tidy, which has the actual compiler’s AST and all the supporting code already.
3
u/TheoreticalDumbass :illuminati: Feb 24 '26
seems funky
1
u/andriyt Feb 24 '26
why not “const_assert”?