Hacker News new | ask | show | jobs
by AshleysBrain 5575 days ago
This reads to me like 'writing good code is hard, and writing even better code is even harder'. With modern C++0x smart pointers (like unique_ptr) and RAII, you can get very high performance and (more) easily exception-safe code. Maybe a lot of C++ exception criticism comes from people still trying to code like C in C++?
3 comments

But your exception-safe C++ code soon becomes bloated with shared_ptr and unique_ptr templates and copy ctors. Almost any C++ function, overloaded operator, or some implicit temporary object's ctor can throw an exception without warning. You must use RAII everywhere for every "resource".

Exception-safe RAII is pretty much an all-or-nothing affair.

Not sure what you mean with copy ctor bloat, but for your_flavor_of_smart_ptr, that's what the typedef keyword is for.

A common idiom I use is to typedef a class's preferred smart-pointer as ptr_t within the class namespace. Passing them around now looks like:

void some_function(my_class::ptr_t);

Exception safe, clear semantics, & no bloat.

"But your exception-safe C++ code soon becomes bloated with shared_ptr and unique_ptr templates and copy ctors."

You need to prove that, I think it's a bogus claim. There is no reason why good template code should be any larger than its hand-written equivalent. Any decent optimizing compiler will take care of the rest.

"modern C++" has changed meanings so many times in the past 10 years that it's not funny anymore. there's always some "modern" solution in C++ that ends up causing more problems, leading to further "modern" solutions that end up... you get my point.

some folks have decided to get off the C++ feature treadmill and go back to, well... getting things done with solid languages (e.g. C) instead of learning about the latest C++ non-solutions to non-problems.

C++ isn't a language like Java where language features and coding styles are handed down from on high. You get to -- you have to -- make your own decisions about which language features are useful for which purposes. Announcements of new C++ features are not, and never were, declarations that the "right way to code" was about to change. Nobody forced you to change the way you coded except by offering better ways, and what's the harm in that? Your "feature treadmill" is not compulsory unless you compulsively keep up with the Joneses (the Alexandrescus? but apparently he has moved on to D now.) The C++ coding style where I work has been the same for at least five years. The C++ coding style at my old shop evolved a little while I was there, but only because I wrote most of the code and was still learning, not because we were adopting new language features.

Anyway, have fun with C; it is certainly a language where you won't run into solutions to any problems you don't have, or solutions for very many other problems, for that matter.

P.S. Andrei Alexandrescu's book Modern C++ Design was published in 2001. That's ten years ago. How much has changed between then and now?

Really? C++ is perhaps the slowest moving language in my repertoire in terms of 'popular' approaches to solving common problems. Look how long a c++0x specification has taken.

And also, www.boost.org. Don't code c++ without it.

Hmm, well, I'm relatively young so I guess I missed all those broken promises :P Still, I think the "trying to code like C in C++" point still stands.
A compressed version can be obtained simply by comparing the initial release of C++ to modern C++, and recalling that the initial version of C++ itself shipped with, well, pretty much the same set of promises that modern C++ ships with.
Do you mean when it was called C with Classes in 1979, or when they changed the name to C++ in 1983? That's about three decades of change either way. A book about the history and evolution of C++ came out in 1994, a year before the first public release of Java. More time has passed since that book was released than between the invention of C with Classes and the publication of that book. When a language has been around for thirty years, it's hard to perceive its rate of change correctly relative to other languages. It would be best compared to Perl, which is only eight years younger, and which is also still thriving. C++ is actually a pretty slow-moving language.
I think you could replace C++ with many, many things involving computers in that sentence.
Well, if you're ever bored, read this book: http://www.amazon.com/Modern-Design-Generic-Programming-Patt...
Exactly.