| ▲ | uecker 6 hours ago |
| The "stronger type system" is mostly a myth in my opinion. It was true in the past in pre-prototype C. The void pointer rules are better in C IMHO as they avoid unneeded casts (that then remove more type safety) and FAMs and variably-modified types can express things C++ simply can't do well. |
|
| ▲ | spacechild1 an hour ago | parent | next [-] |
| I don't get your point at all. C++ has different casting operators (static_cast, const_cast, reinterpret_cast) that are strictly safer than C-style casts. Also, let's not forget that implicit casts between unrelated pointer types is only a warning in C. Fortunately, modern C compilers started treating it as an error by default because it caused so much harm: https://gcc.gnu.org/gcc-14/porting_to.html. In C++ this was always a compiler error. |
|
| ▲ | skrebbel 4 hours ago | parent | prev | next [-] |
| I don’t understand your point at all, C++ objectively has a much stronger type system. It’s turing complete! I’m not arguing that that’s better, or worse, but it’s definitely true and by no means a myth. |
| |
| ▲ | lelanthran 3 hours ago | parent [-] | | I don't think GP meant "it's completely made up", I think he meant the distinction doesn't matter most of the time. I.e. most of the time the typing in real C++ code isn't meaningfully stronger than that found in C code. | | |
| ▲ | uecker 2 hours ago | parent [-] | | More complex != stronger. A weak type system would imply that the type systems forgives type mismatches. C had this before it imported prototypes from C++ where you could call a function without declaring it first and if you got it wrong you got some error or crash. The only part where I think C++'s type system is meaningfully stronger than C are enumeration types. |
|
|
|
| ▲ | flohofwoe 4 hours ago | parent | prev [-] |
| > The void pointer rules are better in C IMHO as they avoid unneeded casts ...so much this! A void pointer is an "any-pointer" by design. It shouldn't require casting from and to specific pointer types, that defeats the whole point of having void pointers in the first place. |
| |
| ▲ | TuxSH 4 hours ago | parent [-] | | > It shouldn't require casting from and to specific pointer types You don't need to explicitly cast T* to void* (guaranteed to be safe), you only need to cast when converting out of void*. The rules are basically the same as casting between pointer-to-derived-class and pointer-to-base-class and they make sense. | | |
| ▲ | uecker 2 hours ago | parent [-] | | They make sense but reduce type safety, because once you add the cast the case might hide some real typing issue. I sympathize with the idea that the down-cast should be explicit though. | | |
| ▲ | spacechild1 an hour ago | parent [-] | | > They make sense but reduce type safety Yes, downcasting can be unsafe and should be used carefully, but what's the alternative? At least in C++ you can't cast between unrelated types without an explicit reinterpret_cast (or C-style cast). | | |
| ▲ | chrchang523 38 minutes ago | parent [-] | | and you CAN use static_cast to convert from void*; this silently keeps working if you refactor the void* into a matching-type pointer later, while raising a compilation error if you refactor to a different-type pointer. | | |
| ▲ | uecker 14 minutes ago | parent [-] | | Yes, a static_cast would be safe, but then most C++ seems to use a C-style cast because it is less clunky and then is less safe than the corresponding C code. The issue is not that the downcast is unsafe (it is, but once you have a void pointer you already accepted this), but that it becomes even less safe by adding a C-style cast. It is also not clear what is gained by forcing programmers to add a cast. Void pointers should be used sparingly anyway. |
|
|
|
|
|