3 ms·
In my opinion, many C++ projects would be simpler, cheaper, safer, and better maintained, if were written in C (with adequate libraries, e.g. for strings, vecto
by faragon 10y ago
In my opinion, many C++ projects would be simpler, cheaper, safer, and better maintained, if were written in C (with adequate libraries, e.g. for strings, vectors, maps, threads, timers, sockets, etc.).
- sseagull 10y agoA vector/map of what? How do I have a map of some key to a custom structure? How do you do this simply without templates? Separate libraries? Macro hell? How do you handle types which need to free memory without destructors? Manually loop over the vector and free stuff every time one goes out of scope? That doesn't seem safer to me.
- faragon 10y ago>>A vector/map of what? How do I have a map of some key to a custom structure? How do you do this simply without templates? Separate libraries? Macro hell? Separate libraries, without "macro hell". >>How do you handle types which need to free memory without destructors? Manually loop over the vector and free stuff every time one goes out of scope? That doesn't seem safer to me. Providing both heap and stack based allocation. E.g. allocating a vector in the stack would require no explicit de-allocation (example: https://github.com/faragon/libsrt https://github.com/faragon/libsrt). For heap allocation, you would need to call some sometype_free(&a) function, obviously (there is no RAII in C).
- aksx 10y ago>For heap allocation, you would need to call some sometype_free(&a) function i find '__attribute__((cleanup (fn)))' fascinating in C, need to try it in some production code though.
- sseagull 10y agoYes, but it would still be hard to support custom types, wouldn't it? Ie in your library, I would have to use pointers, which might be unsafe? Adding types to a library like that (like double) causes the amount of code to grow dramatically (maybe even n^2 for maps). You're library does look nice for what it does, and may have to look into it for some projects (I do have some C projects). Thanks!
- faragon 10y agoThat library is still in beta stage, there are others. For maps I've implemented integer, pointers, and strings, without code bloat (tree implementation is common, without macro-templates). I'm planning adding some generic type support for fixed-size elements, while keeping internal tree pointer-less and both stack and heap allocation possible, with minimal code bloat/duplication.
- knocte 10y agofacepalm
- IshKebab 10y agoI guess you're joking, but just in case, C++ prevents entire classes of errors that are almost guaranteed in C projects.
- faragon 10y agoI was not joking. Also, C++ introduces other classes of errors. There is no silver bullet.
- papaf 10y agoI started converting picol to C++14 yesterday: http://antirez.com/picol/picol.c.txt I have been programming in C++ for over 20 years and my version is bigger :-( Its nice not having to malloc/free but the moment I turned the structs to classes I was battling the bloat. I still enjoy programming in C++ but there is a lot of truth in the criticism that programs written in C are simpler.
- faragon 10y agoI've been writing C++ for more than 20 years, too. In perspective, I made some mistakes when choosing writing many C++ components that could have been better/simpler/cheaper in plain C, with some abstraction via libraries (e.g. because complexes of not being seen as "smart enough", etc.). Writing simple/compact programs is hard both in C and C++, although, from my experience, bloat comes much more from C++ (inlining, etc.). BTW, Salvatore Sanfilippo (antirez) is a very sharp programmer, able to write programs without redundancy and look easy (which is very difficult to achieve).