4 ms·
The C family has already evolved in this direction decades ago. Have you heard of C++ (Cee Plus Plus)? It is production-ready; if you want a dialect of C with
by kazinator 7y ago
The C family has already evolved in this direction decades ago. Have you heard of C++ (Cee Plus Plus)?
It is production-ready; if you want a dialect of C with arrays that know their length, you can use C++. If you wanted a dialect of C in 1993 with arrays that know their length for use in a production app you could also have used C++ then.
The problem with all these "can we add X to C" is that there is always an implicit "... but please let us not add Y, Z and W, because that would start to turn C into C++, which we all agree that we definitely don't want or need."
The kicker is that everyone wants a different X.
Elsewhere in this thread, I noticed someone is asking for namespace { } and so it goes.
C++ is the result --- is that version of the C language --- where most of the crazy "can you add this to C" proposals have converged and materialized. "Yes" was said to a lot of proposals over the years. C++ users had to accept features they don't like that other people wanted, and had to learn them so they could understand C++ programs in the wild, not just their own programs.
- apotheon 7y agoC++ introduces a shit-ton of stuff that one often doesn't want, and even Bjarne Stroustrup (who many content has never seen a language feature he didn't want) has been a little alarmed at the sheer mass of cruft being crammed into recent updates to the standard. I know many C++ people think C++ is pure improvement over C in all contexts and manners, but it's not. It's different, and there are features implemented in C++ and not in C that could be added to C without damaging C's particular areas of greatest value, and many other features in C++ that would be pretty bad for some of C's most important use cases. C shouldn't turn into C++, or even C++ Lite™, but it shouldn't remain strictly unchanging for all eternity, either. It should just always strive to be a better C, conservatively, because its niche is one where conservative advancement is important. Some way to adopt programming practices that guaranteee consistent management of array and pointer length -- not just write code to check it, but actually guarantee it -- would, I think, perfectly fit the needs of conservative advancement suitable to C's most important niche(s). It may not take the form of a Rust-like "fat pointer". It may just be the ability to tell the compiler to enforce a particular constraint for relationships between specific struct fields/members (as someone else in this discussion suggested), in a backward-compatible manner such that the exact same code would compile in an older-standard compiler -- a very conservative approach that should, in fact, solve the problem as well as "fat pointers". There are ways to get the actually important upgrades without recreating C++.
- kazinator 7y ago> C++ introduces a shit-ton of stuff that one often doesn't want The point in my comment is that every single item in C++ was wanted and championed by someone, exactly like all the talk about adding this and that to C. > C shouldn't turn into C++ Well, C did turn into C++. The entity that gave forth C++ is C. Analogy: when we say "apes turned into humans", we don't mean that apes don't exist any more or are not continuing to evolve. Since C++ is here, there is no need for C to turn into another C++ again. A good way to have a C++ with fewer features would be to trim from C++ rather than add to C.
- nicoburns 7y agoSure, but theres a vast space between the C and C++ approaches. You don't have to say yes to everything to say yes to a few things. I would suggest that better arrays are an example of something that pretty much everybody wants.
- pjmlp 7y agoApparently not everyone, otherwise it would be part of ISO C already, and it hasn't been for lack of trying.
- apotheon 7y agoNot literally everyone, I would think, but the previous statement could, in theory, still be true. It would just require some people to want something else, conflicting with that desire, even more. I know, this is pedantic, I suppose. Mea culpa.
- simias 7y agoBut if you want better arrays you want operator overload to be able to use these arrays as 1st class citizens without having to use array_get(arr, 3), array_len(arr), array_concatenate(arr1, arr2) etc... You want to be able to write "arr[3]", "arr.len()", "arr1 += arr2" etc... To implement operator overload you might need to add the concept of references. If you want your arrays type-safe you'll need dark macro magic (actually possible in the latest standards I think) or proper templates/generics. If you really want to make your arrays convenient to use you'll want destructors and RAII. Then you'd like to be able to conveniently search, sort and filter those arrays. That's clunky without lambdas. And once you get all that, why not move semantics and... Conversely if you don't want any of this what's wrong with: struct my_array { my_type_t *buf; size_t len; } I don't think it's worth wasting time standardizing that, especially since I'd probably hardly ever use that since it doesn't really offer any obvious benefits and "size_t" is massively overkill in many situations.
- twic 7y ago> if you want a dialect of C with arrays that know their length, you can use C++ C++ doesn't have arrays which know their length.
- zokier 7y agoWhat's std::array then? > combines the performance and accessibility of a C-style array with the benefits of a standard container, such as knowing its own size https://en.cppreference.com/w/cpp/container/array https://en.cppreference.com/w/cpp/container/array
- kevin_thibedeau 7y agoThey're objects that mostly behave like arrays. You can't index element two of std::array foo as 1[foo] since it isn't an actual C array.
- saagarjha 7y agoOk, but who actually uses that?
- kevin_thibedeau 7y agoThe point is to demonstrate that std::array isn't an array.
- saagarjha 7y agoOk, fair. But for almost all practical purposes, std::array is an appropriate array replacement.
- kazinator 7y agoWhat makes you so sure that if C got better arrays, those would be arrays, supporting a[i] i[a] commutativity and all? That is predicated on equivalence to *(a + i) where a is a dumb pointer whose displacement commutes.