4 ms·
That weird feeling when you realise that the people you hang out with form such a weird niche that something considered common knowledge among you is being desc
by Arch-TK 11mo ago
That weird feeling when you realise that the people you hang out with form such a weird niche that something considered common knowledge among you is being described as "buried deep within the C standard".
What's noteworthy is that the compiler isn't required to generate a warning if the array is too small. That's just GCC being generous with its help. The official stance is that it's simply undefined behaviour to pass a pointer to an object which is too small (yes, only to pass, even if you don't access it).
- loeg 11mo agoThe other fun wart with `static` is C++ doesn't support it. So it has to be macro'd out in headers shared with C++. https://godbolt.org/z/z9EEcrYT6 https://godbolt.org/z/z9EEcrYT6
- pjmlp 11mo agoAnd probably never will, because C++ compatibility with C beyond what was done initially, is to one be close as possible but not at the expense of better alternatives that the language already offers. Thus std::array, std::span, std::string, std::string_view, std::vector, with hardned options turned on. For the static thing, the right way in C++ is to use a template parameter, template<typename T, int size> int foo(T (&ary)[size]) { return size; } -- https://godbolt.org/z/MhccKWocE https://godbolt.org/z/MhccKWocE If you want to get fancy, you might make use of concepts, or constexpr to validate size at compile time.
- loeg 11mo agoI guess. I think it's mostly not a very useful part of C, so it doesn't see much adoption in C anyway. In these applications, size and T are fixed -- you'd just take `std::span<uint8_t, XCHACHA20POLY1305_NONCE_SIZE>` rather than templating.
- flohofwoe 11mo agoNot surprising and not a "wart". C and C++ have diverged since the mid-90s and are two very different languages now. E.g. trying to build C code with a C++ compiler really doesn't make much sense anymore (since about 20 years).
- loeg 11mo agoPeople still routinely want to incorporate C libraries in C++ code, using C headers. It mostly works.
- Arch-TK 11mo agoSure, but it works mostly by intentional hobbling of headers. Meanwhile in other languages you just write/generate bindings and they survive it.
- loeg 10mo agoThe most important C feature that C++ lacked for a long time was C99 designated initializers, but C++ finally supports them in C++20[0]. Other than that... I'm not sure what hobbling you have in mind. Many C23 features come directly from earlier C++ standards (auto, attribute syntax, nullptr, binary literals, true/false keywords). VLAs? Though these are optional in newer C standards, too. [0]: https://en.cppreference.com/w/cpp/language/aggregate_initialization.html#Designated_initializers https://en.cppreference.com/w/cpp/language/aggregate_initial...
- uecker 10mo agoFor me it is complex and variably modified types (mandatory in C23).
- flohofwoe 10mo ago> C99 designated initializers, but C++ finally supports them in C++20 Unfortunately C++20 designated init has been butchered so much compared to the C99 that it is pretty much useless in practice except for the most trivial structs (for instance designators must appear in order of declaration, array item init is completely missing, designator chaining doesn't work ... and those are only the most important things).
- flohofwoe 11mo agoA lot of "modern" C features (e.g. added after ca 1995) are unknown to C++ devs, I would have expected that at least the Linux kernel devs know their language though ;)