4 ms·
Care to elaborate? Keep in mind that the "modern C++" mentioned involves the use of things like RAII, smart pointers, the STL and Boost, rvalue references, mov
by PommeDeTerre 14y ago
Care to elaborate?
Keep in mind that the "modern C++" mentioned involves the use of things like RAII, smart pointers, the STL and Boost, rvalue references, move constructors, and the buffer overflow production offered by the major C++ compilers.
The use of pointers and direct pointer manipulation, for instance, can often be completely avoided with ease these days. That cuts out a large number of potential problems.
It's a much different situation than it was in the past.
- huhtenberg 14y agoSafety implies clarity. Smart pointers, Boost, rvalue references and move constructors is exactly the things that obscure the hell out of the code, frequently making it incomprehensible to anyone but the author. Crashing on a dereferenced NULL pointer is peanuts compared to clusterf#ck of logic that stems from a mess in the dev's head. C++ is less safe because its adepts, on average, are sloppier and messier than those who favor other languages (C included).
- to3m 14y agoYou're right that people tend to overestimate the difficulty of debugging crashes and memory leaks. A lot of C++ code I've seen has tried to move heaven and earth to ensure that mistakes are impossible, at the cost of introducing a lot of rather expensive (time to write, time to execute, time to maintain) infrastructure - or baggage, if you're unkind - that also make things unclear in the debugger. This isn't quite a waste of time, but it's been my own experience that it's cheaper to just make things as straightforward as you can, then simply ask everybody to try not to make mistakes. Often you can build in systems that make it easy to diagnose problems when they occur, or just rely on people's debugging skills. Some programmers consider this a total cop-out - they're right, I should be ashamed - but I've found the overall TCO to be lower. You might have to find this out the hard way to believe it. The worst thing is that some people think the alternative should be C ;)
- betterunix 14y ago"The use of pointers and direct pointer manipulation, for instance, can often be completely avoided with ease these days" Really? I am pretty sure there are a large number of STL implementations that give you a pointer when you ask for a vector iterator. I suppose you could avoid iterating on vectors, but you would also have to avoid operator[] (which by default performs no bounds checking, so the pointer arithmetic is just as unsafe as it would have been with a primitive array type); you'd be stuck with at(), which few compilers will optimize even if it could be shown that the index will never be out of bounds. "That cuts out a large number of potential problems" Yet the language encourages programmers to do things the unsafe way. It is a lot less effort to use a low-level pointer type, a fixed-width integer type, etc. than to use smart pointers, STL containers, iterators, Boost (which is not even standard), and so forth. Just as an example, a C++ programmer must first choose between multiple smart pointer types (where should you break your pointer cycle with a weak_ptr?), whereas a primitive pointer is easily accessible and requires no decision-making (this was the pattern with casting: C-style casts are much more convenient than static_cast, dynamic_cast, or const_cast, and so you saw C-style casts being used all over the place). While a Java or Python programmer on a tight deadline might produce slow code, a C++ programmer will produce unsafe code that is far more likely to have bugs (even if the high-level design is bug-free).
- alexchamberlain 14y agoCompletely agree. Pointer manipulation is a sign you should grab a coffee and chat it over with a friend. Sometimes necessary, of course, but that is the beauty of C++.
- tptacek 14y agoYou are arguing that C++ is safer now than it was in the past. Sure, but that's not responsive to my argument, which is simpler than that.