Archive
The Lost Decade?
I think you’ll be hard pressed to find many knowledgeable C++ programmers who won’t admit that managed languages provide higher per-programmer productivity than native languages (because they’re easier to learn, have bigger libraries, and are not as “picky“). Likewise, I think you won’t find many “reasonable” managed language advocates who won’t admit that native language programs are more efficient (smaller and faster for a given solution) than their managed language counterparts. Having said that, take a look at this chart:
According to Herb Sutter, efficiency has(will) usurped(usurp) productivity as the dominating cost factor for software-intensive products in this decade (battery life in mobile devices, power consumption in the data center). Agree?
If you’re interested in watching the video and/or downloading Herb’s slides, here’s the link: “C++ and Beyond 2011: Herb Sutter – Why C++?“.
Standard, Portable C++ Concurrency
Recently, I downloaded the Microsoft Visual Studio 11 IDE Beta in order to start experimenting with some C++11 features. Lo and behold, standard and portable concurrency is now supported:
At least on Windows, there’s no need to use the Win API, Boost.Thread or ACE or any other third party library in the future to write multi-core friendly, multi-threaded C++ apps. I don’t know when GCC and/or CLANG will ship with the standard C++11 concurrency libs. Do you?
By the way, a series of quick tests verified that lambdas, strictly typed enums, auto, nullptr, std::array, std::regex, and std::atomic work. Initializer lists, raw string literals, “using” as typedef, and range-based for loops don’t work yet.
Broken Books
With the addition of features like auto, initializer lists, lambdas, and smart pointers, a lot of C++98 programming idioms and guidance have become obsolete now that C++11 is here. Thus, as Herb Sutter said at GoingNative 2012, a boatload of code examples in the best C++ books are now “broken“.
As a result, new and revamped versions of the books will take awhile to become available. Here’s Herb’s “estimated time of arrival” for several stalwart books:
In the meantime, you can feast your eyes on, and feed the left side of your brain with, these links:
- Bjarne Stroustrup’s C++11 FAQ page
- Wikipedia C++11
- Scott Meyers Overview Of C++11
- C++11 Features To GCC Compiler Map
- C++ Standards Committee Home Page
- GoingNative 2012
- C++11 Features in Visual C++ 11
- Clang Compiler C++11 Support
- Writing Modern C++ Code (Herb Sutter)
Do you know of any other good links to add to this post?
Concurrency Support
Assuming that I remain a lowly, banana-eating programmer and I don’t catch the wanna-be-uh-manager-supervisor-director-executive fever, I’m excited about the new features and library additions provided in the C++11 standard.
Specifically, I’m thrilled by the support for “dangerous” multi-threaded programming that C++11 serves up.
For more info on the what, why, and how of these features and library additions, check out Scott Meyers’ pre-book training package, Anthony Williams’ new book, and Bjarne’s C++11 FAQ page.
One Chart Summary
While flipping through Herb Sutter’s “C++ And Beyond” keynote speech charts, I stumbled upon this fabulous one chart summary of all (almost all?) the new features available in C++11.
Pretty intimidating, but cool, eh?
Just As Fast, But Easier To Write
I love watching Herb Sutter videos on C++. His passion and enthusiasm for the language is infectious. Like Scott Meyers, Herb always explains the “why” and “how” of a language feature in addition to the academically dry “what” .
In this talk, “Writing modern C++ code: how C++ has evolved over the years“, Herb exhibits several side by side code snippets that clearly show much easier it is to write code in C++11 (relative to C++98) without sacrificing efficiency.
Here’s an example (which I hope isn’t too blurry) in which the C++11 code is shorter and obsoletes the need for writing an infamous”new/delete” pair.
Note how Herb uses the new auto, std::make_shared, std::shared_ptr, and anonymous lambda function features of C++11 to shorten the code and minimize the chance of making mistakes. In addition, Herb’s benchmarking tests showed that the C++11 code is actually faster than the C++98 equivalent. I don’t know about you, but I think this is pretty w00t!
I love paradoxes because they twist the logical mind into submission to the cosmos. Thus, I’m gonna leave you with this applicable quote (snipped from Herb’s presentation) by C++ expert and D language co-creator Andrei Alexandrescu:
Note: As a side bonus of watching the video, I found out that the Microsoft Parallel Patterns Library is now available for Linux (via Intel).
Moving, Initializing, And Hotdogging
In an interview with C++ creator Bjarne Stroustrup, Danny Kalev asked Bjarne several questions about the major features being incorporated into the venerable C++ language via the C++0x standardization effort. Here’s one such Q+A exchange:
I know, I know. The “*” should be a “+” in the free function declaration box above. But does the typo really detract from the message being conveyed?
So, how hard is it to write a move constructor implementation? It’s as hard as this:
Notice how the lower level reference-to-a-reference (i.e. rvalue reference) feature in the Matrix move constructor enables the higher level “move” feature to be easily written. This capability to move big stuff around instead of copying the behemoth will be a great addition to the language, especially to library writers, no?
Bjarne’s examples of the new “uniform and universal initialization” C++0x feature jacked me up too:
We can use the {…} notation for every initialization and wherever we initialize an X with {v} we get the same resulting value. That’s a major improvement over C++98’s non-uniform set of alternatives using the =v, ={v}, and (v) notations:
With any new and powerful tool, there’s always the danger of “hotdoggers” conniving to show how much smarter than you they are. Here’s a typically wise and insightful, and universal quote from Bjarne on the topic:
Before we get to benefit from the simplifications offered by C++0x, we may go through a period where too many people try to show off their cleverness by enumerating language rules and digging into the most obscure corners. That can do harm.
You’re not one of “those” people are you? I’m not. Luckily, I ain’t smart enough to be a hotdogger. But if I was brilliant…….
Note: I know that the code above is really C, but it was the first googled C++ example I stumbled across.
Related Articles
- Bjarne Stroustrup Reflects On 25 Years of C++ (developers.slashdot.org)
- Bjarne Stroustrup on C++ Inception (thebitsource.com)
Scott Meyers On C++0x
From what I know (which is very little), it looks like the C++0x programming standard will finally be released to the world as C++0B this spring. As part of preparing for this release, I recently listened to an interview with “Effective” Scott Meyers on Software Engineering Radio.
In case you find yourself bored, and with an hour of free time on your hands, check out my audio + notes pencast recording of the interview on the Livescribe smart pen site: here. (Clicking on the pic below won’t start the pencast in-situ. I haven’t figured out how to embed pencasts into this blog yet).
The interview was an interesting mix of good technical information from Scott, triggered by great questions from the interviewer. Nevertheless, at several points during the interview, the interviewer seemed to interject sarcastic commentary that showed his disdain for the language. He obviously wasn’t/isn’t a C++ language fan, but he still did a great job with his questions. The interview never veered off into a “your language sux and my language rules” religious war and the interviewer’s snarky remarks kept the interview refreshing:
“Why doesn’t C++ have garbage collection? …… Because then there would be nothing left.”
In summary, Scott used his considerable teaching talent to clearly and concisely explain these key features of the “C++0B” standard:
- concurrency
- the “auto” keyword
- move semantics using rvalue references
- variadic templates
- lambda functions
- uniform initialization syntax
- tuples (or as the interviewer pronounced, “two-pulls” 🙂 )
If C++ is your programming language of choice and you haven’t followed the development of the new standard, now may be the time to start wading in? The Meyers interview, this excellent Wikipedia C++0x starter page, and Bjarne Stroustrup’s C++0x FAQ page may help.
Related Articles
- An Interview with C++ Creator Bjarne Stroustrup (codeguru.com)
- Type Inference vs. Static/Dynamic Typing (herbsutter.com)
Final, Not Virtual
When writing C++ code, I used to have the habit of tagging every destructor in every class I wrote as “virtual“. I knew that doing so added memory overhead to each object of a class (for storing a pointer to the vtable), but I did it “just in case” I decided to use a class as a base in the future. I was afraid that If I didn’t designate a class dtor as “virtual” from the get go, I might forget to label it as such if I decided later on I needed to use the class as a base.
I have actually made the above mentioned “inheriting-from-a-class-that-doesn’t-have-a-virtual-dtor” mistake several times before – only to discover my faux pas at runtime during unit testing. However, since C++11 added the “final” keyword to the core language to explicitly prevent unintended class derivation (and/or function overriding), I’ve stopped tagging all my dtors as “virtual“. I now tag all of my newly written classes as “final” from the start. With this new habit, if I later decide to use a “final” class as a base class, my compiler pal will diligently scold me that I can’t inherit from a “final” class. Subsequently, when I remove the “final” tag, it always occurs to me to also change the dtor to “virtual“.
#include <iostream>
class ValueType final {
public:
~ValueType() = default;
};
class BaseType {
public:
virtual ~BaseType() = default;
};
int main() {
using namespace std;
cout << " sizeof ValueType = "
<< sizeof(ValueType) << " bytes" << endl
<< " sizeof BaseType = "
<< sizeof(BaseType) << " bytes" << endl;
}
















