Advertisement

Dive in to C++ and survive

Christopher White with Kelly Deltoro-White

February 16, 2009

Christopher White with Kelly Deltoro-WhiteFebruary 16, 2009

If you're anything like me, when you run into a little programming problem you just trot down to the library and pick up a couple of books/manuals on whatever it is you need to master. Do a little reading and POW! You've got the tools you need.

When I ran into problems with my first major C++ project, however, I discovered that each new "solution" just slammed me into another roadblock. I needed answers. But all I got was frustration.

And it looks like I'm not the only one. According to the 2008 Embedded Market Study, C++ use has lagged behind C and the numbers have not significantly improved. In 2005, only 26% of those surveyed used C++ (compared to 51% with C) and while C use rose about six percentage points over the next three years, C++ use fluctuated before it settled into a mere three percent increase.

No surprise, given the frustration factor, but the fact of the matter is C++ is an amazingly useful tool in embedded design. But before you can use its awesome powers, you just have to wrap your mind around one little detail:   C++ isn't a programming language; it's a worldwide research project running about in disguise.

Here's the lowdown. Reading compiler manuals will only get you so far. The bulk of C++ is documented in papers, presentations, and (most importantly) the experience of its users. In fact, the range of legal C++ code is so wide that you have to write idiomatically to be understood by other programmers (see References [2, 3, 4, 5]). You want to be good? Then you've got to get involved.

So, come on. Let's dive in and get our hands dirty.

Many of us remember when the number one (frustrating) feature of C++ was that its portable code wasn't actually portable. These days, C++ is much more portable on desktop and embedded platforms, but portability still isn't a given [7].

Your best bet? You got it - head to the library! C++ library authors and vendors are C++'s QC department. To see how your compiler measures up, start with the Boost library regression test summary [8] and the Dinkumware standard-library manual [10].

For many platforms [11], GNU GCC is a good bet for a conforming compiler with a price that just can't be beat. If there's no C++ compiler for your platform, try Comeau C++ [9]. It claims to track the standard very closely, and compiles C++ to C for your favorite toolchain. While I haven't tried it myself, the reviews are good.

The compiler's main business is managing names, not generating assembly. The code int i; doesn't necessarily allocate space for an integer. Instead it gives the name i a new property: it can refer to something of int type in the current scope.

This matters when you use overloading and templates, when what a name refers to depends on the context in which it is used. Template matching and overload resolution are designed to make the common cases Just Work, but this can lead to confusing errors about ambiguous function calls (Appendix B in [11]).

Partial specialization, where a template can behave differently for subgroups of its arguments, adds even more power with its attendant complexity (Sections 1.5 and 2.2 in [6]). Unfortunately, there's no shortcut here, so start simply and learn the rules as you go.

< Previous
Page 1 of 4
Next >

Loading comments...