Sujet : Re: C23 thoughts and opinions
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 22. May 2024, 23:53:41
Autres entêtes
Organisation : None to speak of
Message-ID : <87msoh5uh6.fsf@nosuchdomain.example.com>
References : 1 2 3
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
David Brown <
david.brown@hesbynett.no> writes:
On 22/05/2024 19:42, Thiago Adams wrote:
[...]
- nullptr
>
I am fond of nullptr in C++, and will use it in C. Like most of the
C23 changes, it's not a big issue - after all, you get a lot of the
same effect with "#define nullptr (void*)(0)" or similar. But it
means your code has a visual distinction between the integer 0 and a
null pointer, and also lets the compiler or other static checking
system check better than using NULL would. (And I don't like NULL - I
dislike all-caps identifiers in general.)
Quibble: That should be
#define nullptr ((void*)0)
For example, this doesn't produce a syntax error for `sizeof nullptr`.
Better:
#if __STDC_VERSION__ < 202311L
#define nullptr ((void*)0)
#endif
C23's nullptr is of type nullptr_t, not void*. But you'd probably have
to go out of your way for that to be an issue (e.g., using nullptr in a
generic selection).
[...]
- constexpr
>
I will definitely use that. Sometimes I want a constant expression
for things like array sizes or static initialisers, and want to
calculate it. constexpr gives you that without having to resort to
macros. (I'd perhaps be even happier if I could just use const, as I
can in C++.)
But const doesn't mean constant. It means read-only.
`const int r = rand();` is perfectly valid.
I dislike the C++ hack of making N a constant expression given
`const int N = 42;`; constexpr made that unnecessary. C23 makes the
same (IMHO) mistake.
If I had a time machine, I'd spell "const" as "readonly" and make
"const" mean what "constexpr" now means (evaluated at compile time).
[...]
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */