7 ms·
Let's say you churn something out in python and it works the way you like but you need it to complete faster. You move it to C/C++ (after not being able to spe
by djoldman 6y ago
Let's say you churn something out in python and it works the way you like but you need it to complete faster.
You move it to C/C++ (after not being able to speed up the python code using FFI or something else).
Do you go C or C++?
Even if I'm writing C++, I find myself using a restricted subset that is mostly C.
- Sebb767 6y agoBut 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
- dgellow 6y agoI guess it depends what you mean by that. If you have a program in "mostly C", you can start using RAII to manage your ressources. Then use std::unique_ptr, std::shared_ptr, and references instead of raw pointers. And namespaces. That already brings you to a very nice place without shifting completely to modern C++. You don't have to use all the features of C++ if you don't need them.
- j1elo 6y agoThe problem of this phrase, which we all repeat (myself included), is that in reality you would need at least a superficial grasp of all of them, to make an informed decision of which ones you need (or want) to include in your project. Then the thing grows to a team of 20, and you find yourself applying restrictive rules about which subset of C++ is admissible, because otherwise everyone will consider a different subset for their own code. "You don't need to use all of the language" is a false claim that doesn't go too far without adding extra friction to the project management.
- dgellow 6y agoYep, that's fair. I wouldn't call it "false claim", because I don't it's actually incorrect, you can be perfectly effective and go quite far with just a small selective subset of the language ("A tour of C++, 2nd edition" gives a good overview of this subset IMHO[0]), but the claim is ignoring the mental overhead and decision paralysis induced by C++ complexity. [0]: https://www.stroustrup.com/tour2.html https://www.stroustrup.com/tour2.html
- j1elo 6y agoI agree, it's not a false claim. You definitely put it in better words.
- whimsicalism 6y agoIf you're using raw pointers in 2021, I think that is a mistake
- adamrezich 6y agoI'm no C++ expert, but I like mucking about with game projects in C++ every few years, and I'll never not use raw pointers. smart pointers in C++, as well as a lot of these newer language features, really confuse me as to what niche C++ is supposed to fit in. when I use mostly-C-style-C++, I'm using it because I want to drill down into the nitty-gritty, I want to access raw pointers and feel free to do whatever I want with them. I don't want higher-level abstractions over something like pointers, I'm a big boy, I can manage allocating and freeing memory when I need to, I can write my own memory managers and so forth. maybe this is just something domain-specific to game development but I have seen many experienced C/C++ programmers advocate for this as the way to do things in C++, if you're going to use C++, and my (minimal compared to these people) experience (both before and after hearing these perspectives) lines up with what they have to say. if I was using C++ to write business applications or something, like one would use C# or Java or whatever, then yeah, smart pointers seem like they would be useful in that specific domain... but at that point, why not just use one of those languages, or a language like it? I'm probably going to try zig for my next endeavor into lower-level game development because that language, while different from the C-style-C++ I'm used to, seems much more in line with the kind of programming I'm looking to do, compared to modern C++. I don't want RAII, smart pointers, and all that conceptual overhead jazz. I want to allocate memory, run operations on said memory, then free said memory. I kinda miss just doing stuff in C89.
- Koshkin 6y ago> then free said memory Resource deallocation becomes extremely difficult in the context of (various types of) exception handling. C++ destructors make this a breeze.
- adamrezich 6y agoabsolutely—but I'm also not a fan of exceptions either & don't use them when I write C++. if you write your C++ in a mostly C-style, only taking C++ features here and there as you need them, the complexity of your code is often greatly reduced. again, this may only apply to game development—I haven't used C++ for anything else sizeable, aside from school assignments years ago.
- saboot 6y agoIn your example, of interfacing with Python, yes I would usually go for C because to interact with CPython needs a C ABI in the end. C++ requires another tool to get there (pybind, swig, etc).
- pjmlp 6y agoextern "C" { // my C++ code } Big deal.
- jcelerier 6y agobut pybind11 makes interop so much more trivial between C++ and python. passing std::vector, std::string, custom types, etc... it all just works and with much less errors possible than the C Python API. Why would you subject yourself to it ?
- pjmlp 6y agoI always go C++, because it offers the type safety that is impossible with bare bones C.
- nsajko 6y agoCheck out at least: * auto * namespaces * templates * lambdas The standard library is much more useful and expansive than C's. And if you really care about performance, C++ is the obvious choice (but it does have its pitfalls, I think).
- gpderetta 6y agoInterestingly there were a bunch of language alignment papers in the last mailing that proposed adding all the above (IIRC templates only in a simplified form) to C.
- Wohlf 6y agoI tend to have junior devs prototype in Python and we rewrite in C# when necessary.
- Fordec 6y agoI'm eagerly awaiting better supported Rust integration with Python for this very reason. I really don't fancy wading back into C/C++ after being away from it for about 5 years and growing as a developer.
- MetroWind 6y agoDefinitely C++ for me. Just to have templates and things like vector, array, variant, span, if nothing else. But these days I may prefer Rust over the two instead, if it’s not a numeric-heavy program.