4 ms·
C is fine too. I pick a C++ subset that is closer to C than to full modern C++17. Full control over memory access, hardware access and execution. I haven't lear
by erwincoumans 5y ago
C is fine too. I pick a C++ subset that is closer to C than to full modern C++17. Full control over memory access, hardware access and execution. I haven't learned enough Rust yet, that could be fine too, but Rust lacks still many libraries in Robotics and Game development.
What else do you suggest?
- ncmncm 5y agoIn other words, you pick exclusively the worst features of C++, and ignore all the actually useful, helpful features that enable you to write better code. If you just want C, use C.
- Koshkin 5y agoWell, with a C++ compiler there's always hope that they will pick up the good parts, eventually.
- TillE 5y ago"Modern C++ bad" is such a weird HN meme, fortunately it's not an opinion I see a lot elsewhere. Old C++ was awful, the new stuff makes it actually usable. Personally I like how even in embedded code where you don't want to include the standard library, you can still efficiently use language constructs like constexpr, lambdas, etc.
- Koshkin 5y agoCareful with lambdas, they are not without an overhead.
- zozbot234 5y agoLambda is designed to be a zero-overhead feature. In the worst case, it's the same as passing an additional void* ctx argument to a plain old function, which is the most common pattern in C/pre-lambda C++.
- Koshkin 5y agoBut it's not. A lambda is merely syntactic sugar for an object (a functional) and as such it can carry a state (the term is 'closure' in this case); depending on what is being captured, its creation can even involve a heap allocation.
- zozbot234 5y agoNot sure I get your point - the exact same is true of a C function which is passed a void* ctx object, since those objects have to be allocated and their lifecycle has to be managed. It's still a zero overhead feature.
- Koshkin 5y agoThe (potential) overhead lies with 'ctx' of course. The lambda syntax makes it all too easy to forget about it.
- ncmncm 5y agoA pointer argument passed in a register is not what we mean when we say "overhead". Without the lambda, you would instead need to pass the pointer manually, for identically the same cost. But then the compiler would not understand as well what you were doing, and would be unable to optimize it as well. Here, the lambda gives you negative overhead, vs. what you would have written.
- Koshkin 5y agoYou have to look beyond the pointer, of course. (An object and a pointer to it, ‘this’, are two different things.)
- ncmncm 5y agoIt is: "zero-overhead" is defined to mean you could not open-code it yourself any better. If you specify a capture-by-copy, you have specified a copy. If that copy involves an allocation, you have specified an allocation. There is exactly zero extra overhead: in the overwhelmingly most common uses, not even a call through a pointer. The good news is that the compiler knows all about lambdas, so can optimize the hell out of them.
- erwincoumans 5y agoI didn't mean to say "modern C++ is bad", but I'd prefer a small subset that I can be sure no surprises happen: no hidden memory allocations and such. In fact, we use modern C++ and Eigen and CppAd Codegen in some of our projects: the generated C code by tracing a full sim step is about 5 times faster, likely due to no memory allocations and cache friendly memory access.
- ncmncm 5y agoCareful coding to get runtime properties you value is just careful coding. It is not a language subset, and there is strictly negative value in a subset "closer to C". When coding carefully, you can still take full advantage of all of the most powerful features of the language without penalty. Maybe you watch your cache footprint, and use memory supplied by the caller, and avoid branches that would often be mis-predicted. That's just programming.