Sujet : Re: C23 thoughts and opinions
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 23. May 2024, 21:37:19
Autres entêtes
Organisation : None to speak of
Message-ID : <87ttio464g.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
David Brown <
david.brown@hesbynett.no> writes:
On 23/05/2024 00:53, Keith Thompson wrote:
[...]
I dislike the C++ hack of making N a constant expression given
`const int N = 42;`; constexpr made that unnecessary.
>
I find that "hack" convenient at times. But I see what you mean that
it is a "hack", and I agree that "constexpr" makes such a hack
unnecessary. (Ideally, the languages would have used terms such as
"read_only" and "constant" rather than "const" and "constexpr", but
that boat sailed long ago.)
>
C23 makes the
same (IMHO) mistake.
>
I don't think so - as far as I can see, it avoids that mistake (if you
feel the "hack" was a mistake). C23 can't fix the choice of names -
that was from C90.
Apparently I was mistaken. I had thought that C23 made this legal (as
it is in C++):
#include <stdio.h>
int main(void) {
const int n = 42;
switch (n) case n: puts("ok");
}
There was a proposal to do this:
https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Initialized%20const%20Integer%20Declarations.htmland I wrote a response to it:
https://github.com/Keith-S-Thompson/const-proposal-responsebut the proposed wording does not appear in N3220.
(If you want n to be a constant expression, replace "const" with
"constexpr" -- or use a macro or the enum hack if you don't have a C23
compiler.)
We're definitely stuck with the "const" and "constexpr" keywords, but at
least the semantics of "const" are reasonably consistent.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */