5 ms·
But why? C++ has some pretty good upgrades over C - like classes, smart pointers and a better type system. Even if you're not writing idiomatic C++20 code, havi
by Sebb767 6y ago
But why? C++ has some pretty good upgrades over C - like classes, smart pointers and a better type system. Even if you're not writing idiomatic C++20 code, having a better structure alone is worth it IMO.
If you don't need classes, maybe you should look into Rust - it's quite a bit more restrictive, but it allows a nice, functional style with similar performance while avoiding most of the pitfalls and footguns of C.
- huhtenberg 6y agoSmart pointers aren't an upgrade over C per se. It's a coding pattern that has its specific use, but that's definitely not something that's missing from C or a natural extension to how things are generally done in it.
- bluGill 6y agoIf you are porting Python to something else, then smart pointers are a very useful upgrade that save you from a ton of analysis that Python just did for you. Garbage collection does almost the same thing, but you need to deal with python's "with" somehow then.
- nsajko 6y ago> Garbage collection does almost the same thing As far as I understand, smart pointers are actually a somewhat stupid form of garbage collection.
- ncmncm 6y agoWe usually prefer the adjective "deterministic" over "stupid". Simplicity, zero overhead, and "user pays" are virtues. People who complain about C++ smart pointers tend to be the ones who reflexively reach for shared_ptr. (We call that habit "Java Disease".) Most good C++ programs have zero uses of shared_ptr. Also, passing around shared_ptr or unique_ptr in public interfaces puts you in a state of sin.
- Aeolos 6y ago> Also, passing around shared_ptr or unique_ptr in public interfaces puts you in a state of sin. Why is that?
- ncmncm 6y agoIf they are passed by value or move, you are doing complicated things with ownership that deserve to be encapsulated in a named type. If you are passing them by const reference, you would usually better pass a regular pointer or reference to the pointed-to type. If you are passing them by non-const reference, you have bigger problems. In practice, the new type usually starts out as a PIMPL class with just a std::unique_ptr<impl> member. But often, once the type is named, you discover it is a good place to park more operations.
- GuB-42 6y agoI wouldn't call smart pointers an "upgrade". If you are using smart pointers, you are not using the same language. There are many features of C++ that would make a "better C", like namespaces and templates (which are better macros). But as good as they are in other contexts, smart-pointers are the last thing I want in C-like code. Also, an advantage of C over C++ is that it doesn't have a runtime and it doesn't use name mangling. That makes linking much easier and particularly well suited to embedded applications.
- ModernMech 6y agoC has a tiny little runtime: https://en.m.wikipedia.org/wiki/Crt0 https://en.m.wikipedia.org/wiki/Crt0