14 ms·
I think it's a sensible choice. I've seen way too many C codebases rewriting half of the STL or using clunky macro hacks (or worse, half-assed linked lists) whe
by qalmakka 5y ago
I think it's a sensible choice. I've seen way too many C codebases rewriting half of the STL or using clunky macro hacks (or worse, half-assed linked lists) when basically every single platform out there has a copy on the STL available which includes containers and algorithms with excellent performances and are well tested.
It's complicated but it's the only reasonable choice. You can then write your code C-style while compiling it as C++ and nobody will bat an eye.
- FpUser 5y ago>"You can then write your code C-style while compiling it as C++ and nobody will bat an eye." Very practical and reasonable choice.
- calvinmorrison 5y agoC style code plus you can get some nice things like hashmaps without having to implement or find a implementation and instead use std library
- jupp0r 5y agoYou can also statically link libc++. Not much of a binary size increase from static linking because most is template headers.
- account42 5y ago> Not much of a binary size increase from static linking because most is template headers. Unless you use anything that pulls in locale code (e.g. any stream), in which case you will get code for things like date and monetary formatting in your library even if it is never used.
- jupp0r 5y agoLinkers are pretty good at throwing out dead code, so if you don't reference these functions there's a decent chance they won't be linked in when linking statically.
- ziftface 5y agoBut if that's the case couldn't there just be a library that provides all the functionality without having to switch languages? Something like boost but for plain C?
- whaaswijk 5y agoIn theory there could be such a library but in practice I don’t think there is. Maybe because maintaining something like the STL is a non-trivial task.
- mastrsushi 5y agoThat’s pretty much what glib is. There are generic structures.
- lalaland1125 5y agoThe lack of generics for C makes it difficult to write those sorts of libraries in an efficient way.
- ncmncm 5y agoAnd, generally impossible to write C generics so they may confidently be used safely. There are reasons why C++ was necessary.
- yakubin 5y agoSTL has a lot of weird pitfalls. There was std::vector<bool>. Here you can see some pitfalls of std::unordered_map: <https://youtu.be/ncHmEUmJZf4?t=2220 https://youtu.be/ncHmEUmJZf4?t=2220>. The whole talk is interesting to watch. In the beginning you can also see the puzzling design of std::unordered_map (puzzling because of the number of indirections). I'd reach for abseil first: <https://abseil.io/ https://abseil.io/>.
- fnord77 5y agoafter watching that video, I never want to touch C++ again
- db48x 5y agoSeveral CPPCON talks in recent years have reinforced that for me as well. https://www.youtube.com/watch?v=7DTlWPgX6zs https://www.youtube.com/watch?v=7DTlWPgX6zs, for example. Edit: I originally linked to the wrong video (https://www.youtube.com/watch?v=TFMKjL38xAI https://www.youtube.com/watch?v=TFMKjL38xAI), which was a different talk by the same person.
- pjmlp 5y agoThen someone has to do it for you, because for the foresable future there will be plenty of critical libraries, and compiler infrastructure, that won't be available in anything else as implementation language. Hence why I keep my C and C++ skills sharp, even though they aren't my main languages any longer.
- fnord77 5y agoor it is time to replace these critical libraries with something that has the same features and is far less convoluted and far harder to make serious mistakes with. In addition to having experts accidentally make mistakes with `ref ref` like in that first video that resulted in performance degradation due to unwanted copies, C/C++ is a major security risk. Countless vulns are from simple mistakes and complex misunderstanding.
- jjnoakes 5y agoSeems like exception handling and exception safety would be quite tough to achieve if you are just sprinkling STL throughout a mostly C codebase, no?
- kccqzy 5y agoA mostly C codebase using a bit of STL will almost certainly not use exceptions. So there's no need to worry about exception safety.
- dodobirdlord 5y agoWell, you could compile everything with noexcept and not use exceptions, but that would certainly make large chunks of the STL useless.
- _b 5y agoWhat? Everything in the STL works fine without exceptions. Only wrinkle is if one wants to handle out of memory in way besides crashing, although there is generally nothing better to do. Besides, the kernel will often kill processes rather than return malloc failures (see the OOM killer) anyway.
- dodobirdlord 5y agoIIRC, STL constructors throw if they can’t run successfully. I suppose if you’re sure that OOM is the only thing that is going to cause constructors to fail, you can just live with allocation failures causing program termination.
- qalmakka 5y agoThe STL can throw exceptions, but it's easy to avoid that if you use the right patterns (i.e., find() instead of at(), iterators, and so on). If you build your C code as C++, exceptions will flow through it just fine. Nothing will throw anything unless call throw (or you run out of memory).
- 5y ago
- baybal2 5y ago> C++'s std:: tools are more useful than the bespoke tools in libtransmission's C code. Nearly every time I go in to fix or change something in libtransmission, I find myself missing some C++ tool that I've come to take for granted when working in other code, either to avoid reinventing the wheel (e.g. using std::partial_sort() instead of tr_quickfindFirstK(), or std::vector instead of tr_ptrArray) or the better typechecking (e.g. std::stort() instead of qsort()). I think it's more to glib development entering an ice age, than a language issue. Glib had an impressive roadmap list around 8-10 years ago, copying much of STL features, but of course nothing much materialised after GNOME 3 caused an exodus of GTK developers.
- ncmncm 5y agoPatching up C so it can safely play where the big kids do is both pointless and doomed to failure. It is way, way less work to switch to using a better language, instead. Any C program can be converted to build with a C++ compiler with no more than a day's work. Then, you can start in modernizing the active parts, deleting local, generally poorly-tested, cobbled-together utilities as you go.
- pjmlp 5y agoI have been sounding that trumphet since 1993, but there are lots of very hard listeners.