Liste des Groupes | Revenir à cl c |
David Brown <david.brown@hesbynett.no> writes:Indeed.On 22/05/2024 19:42, Thiago Adams wrote:[...]Quibble: That should be- 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.)
#define nullptr ((void*)0)
For example, this doesn't produce a syntax error for `sizeof nullptr`.The use of generics can be an advantage of nullptr here. The use in templates was a prime motivation of introducing nullptr to C++, though I think it is fair to say that templates are very much more popular in C++ than _Generic is in C. But I haven't thought of a real-world use-case yet!
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).
[...]Yes - which is why "constexpr" can be useful.
But const doesn't mean constant. It means read-only.- 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++.)
`const int r = rand();` is perfectly valid.
I dislike the C++ hack of making N a constant expression givenI 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.)
`const int N = 42;`; constexpr made that unnecessary.
C23 makes theI 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.
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).
[...]
Les messages affichés proviennent d'usenet.