Summary:
In this article, Scott Meyers shares his picks for the five most meaningful
Aha! moments in his involvement with C++, along with why he chose them.
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: October 31, 2006 4:27 PM by
|
In this article, Scott Meyers shares his picks for the five most meaningful Aha! moments in his involvement with C++, along with why he chose them. http://www.artima.com/cppsource/top_cpp_aha_moments.htmlWhat were your most important aha! moments in C++?
|
|
|
My most important Aha moment had been when came to know about the function objects.
() operator can be overloaded and the object can itself act as a function !!
|
|
|
Enough already! It's like hearing the same joke over and over.
What's next? The Most Important Piece of Cable that's ever carried C++?
(Please tell me we're not going to go through this a dozen times for every language.)
|
|
|
I never had any aha moments...C++ was always a struggle for me...an endless chase of pointers, analysis of crashes, and things like that!
|
|
|
One of my biggest aha! moments with C++ came when I learned that you could do metaprogramming in C++ (from Todd Veldhuizen's work: http://osl.iu.edu/~tveldhui/papers). I was working with things like 3D graphics at the time, and compilers weren't that very good to do things like automatical loop-unrolling and similar optimisations, so I was thrilled to learn that you could actually make the compiler do this, yourself... As well as other program transformations and computations. As is now well known, C++ happens to have a Turing-complete language at compile-time, which makes theoretically any transformation or computation - where the data is available at compile-time - possible. You may even do it partially at compile-time, partially at run-time, and take advantage of any information existing at compile-time, to make execution more efficient or safe (the latter coming more from static typing, than metaprogramming). In a sense, the C++ template system - with its non-type template parameters - is a little like a dependent type system.
|
|
|
Understanding how to write an exception safe copy assignment operator, using a swap helper function. My first attempts to implement a decent assignment for a non-trivial class failed miserably. I tried (quite literally!) to assign all of its data members, catching all possible exceptions, but I ended up having hopelessly corrupted objects. I'm pretty sure I first read about using a non-throwing swap to implement operator= in an article by Herb Sutter. My initial reaction was "huh?!?" But it was followed quickly by a big "Aha!" :-)
|
|
|
I have to pick metaprogramming as well. And RAII. And "wow, this sure has a lot more power than Java does" which was more a Java Aha moment than a C++ Aha moment ("lemme write this quick one-off; should I use Java or C++? in C++ I'd ..., in Java I'd ..., wait a minute, Java gives me fewer choices!")
|
|
|
My best Aha! moment wasn't directly related to C++ although it uses it. I realised that programming doesn't need functions, execution points or any execution control statements at all. These things actually make programming restrictive. This makes the Turing machine irrelevant and makes your software instantly portable without any VM or anything like that. You just need a small 'kernel' that will handle the low level execution handling for you which will then be linked into your software's executable. In other words, it'll handle the Turing machine side of it. This can be written in C++ or whatever language. It's a 30 year old idea that will probably take another 30 years to come to fruition by the looks of things.
|
|
|
Probably RAII. I don't think any other idiom changed my programming style as much.
|
|
|
While reading 'Object-Oriented Analysis and Design with Applications' by Grady Booch.
|
|
|
My first Aha! moment was when I understood how Singleton design pattern works by making the constructor private and a public static method. Another one was pretty recent when I understood how execute-around method like pattern can be implemented in C++ using "Double Application of Smart Pointers". Given here: http://www.aristeia.com/sdnotes_frames.html
|
|
|
> Probably RAII. I don't think any other idiom changed my > programming style as much.
The same for me! Private copy constructors and assignment operators are closely related to RAII. But the rest of the Aha-list (2 misnomers, a trick that "had little impact") is rather strange.
|
|
|
> I never had any aha moments...C++ was always a struggle > for me...an endless chase of pointers, analysis of > crashes, and things like that!
Absorb RAII and the struggle will go away.
|
|
|
> My best Aha! moment wasn't directly related to C++ > although it uses it. I realised that programming doesn't > need functions, execution points or any execution control > statements at all. These things actually make programming > restrictive. This makes the Turing machine irrelevant and > makes your software instantly portable without any VM or > anything like that. You just need a small 'kernel' that > will handle the low level execution handling for you which > will then be linked into your software's executable. In > other words, it'll handle the Turing machine side of it. > This can be written in C++ or whatever language. It's a > a 30 year old idea that will probably take another 30 > years to come to fruition by the looks of things.
This sounds interesting, could you elaborate on it, or give any pointers to where to learn about this idea?
|
|
|
> > I never had any aha moments...C++ was always a struggle > > for me...an endless chase of pointers, analysis of > > crashes, and things like that!
> Absorb RAII and the struggle will go away.
OMG. I went and looked up RAII and was [hardly?] surprised to see that it is nothing but "clean up in a destructor". Wow, that was a waste of a half hour of reading.
What next? Maybe we could use CFront to add methods to structs? ;-)
The problem in large-scale C++ wasn't cleaning up stuff in a destructor, it was knowing when you could (and when you had to) destroy things. Modern platforms (i.e. what we've been using for the past 10 years) automatically handle this for us perfectly -- in most cases.
Peace.
|
|