According to Hoyle...
C/C++ Language Updates
November 2008
by Jonathan Hoyle
jonhoyle@mac.com
macCompanion
http://www.jonhoyle.com
Early last year, I wrote about the upcoming changes in the C++
language. It has been, by far, my longest article
for macCompanion, detailing most of the changes we can expect to see. (For those interested in a higher level
view, visit my online PowerPoint presentation on the topic.) I will not be
repeating that information here, as the great majority of it remains
unchanged. However, I would like
to give an update on the subject and provide additional links for those
interested in pursuing this further.
C++0x == C++09?
The original specification for C++ was ratified by the ANSI/ISO
committees in 1998, and is thus known as C++98. After a deliberate five years of silence (to assess the
state and usage of C++), a Technical Corrigenda was produced with
recommendations for bug-fixes in the specification, primarily rewording for
clarification. This update to the
language was called C++03 and had almost no real impact on compiler
developers. The only one area that
had a potential impact was formalized requirement that the memory allocation
for a std::vector<> must be contiguous. (As this was the original spirit behind this container class all
along, I know of no compiler vendor which hadn't implemented it this way already.)
In 2004, the ISO committee began accepting changes for real language
updates, the project name being C++0x, as it was anticipated that this
initiative will be finalized sometime in 200x. Last year, people began referring to C++0x as
C++09, as it was becoming increasingly obvious that this specification will not be completed prior to
2009. Unfortunately, there seems
to be a realistic chance that this will not be finalized even before the decade completes,
thus making the working name C++0x possibly out of sync with a 2010
completion. (Bjarne Stroustrup has
made the half-serious suggestion that the x be interpreted hexadecimally, so it
would read C++0A, in the event it does not finish in 2009.)
In any case, the committee members are very much dedicated to
finishing this in the 2009 timeframe, so hopefully we won't have to worry about
that.
What's In, What's Not
Most all of the essential changes appear to be in. These include C99 features (__func__, long long's, variadic macros, and others), Standard Library
enhancements (smart pointers, regular expressions, arrays, tuples, etc.), and
in-class initializers. Also making
the cut are delegating and inheriting constructors, nullptr, auto & decltype, rvalues references and concepts. Unfortunately, much less of the
threading constructs will be found in C++09, aside from thread local
storage. Sadly, some of the mathematical
additions are also being held off from this release.
For a more detailed analysis of features being included (as of
August 2008), read Bjarne Stroustrup's interview with DevX.com entitled The State of the Language.
Forgetting to Take Out the Garbage
The biggest item not making it in C++0x is Garbage Collection. Although this is still on tap for a
future rev of the C++ standard, it is very disappointing that this one is
missed out. One of the key
benefits of newer C++ like languages (such as Java and C#) are their built-in
garbage collection capabilities. Even older languages (like Objective-C) are being updated to include this
feature, just so they can stay relevant. Without GC in C++0x, there is the risk that there will not be a C++1x
... or if there is, it will no longer be relevant.
For the C++ programmer wishing to use pointers in a modern way, the
new smart pointer shared_ptr<> (borrowed from the Boost framework) should be an adequate
substitution. Thus instead of
allocating a heap-based class in the tradition way:
ObjectType *myObjectPtr
= new ObjectType;
you should declare it as follows:
shared_ptr<ObjectType> myObjectPtr = new ObjectType;
By doing so, myObjectPtr is now "smart" and can dispose of
itself when it is no longer needed.
What About C?
Long before the advent of C++, the C Language reigned supreme. Particularly as the 1980's progressed,
C's popularity ballooned, and it quickly became the lingua franca of
programming languages. Unfortunately, competing C compilers each had differing implementations,
making it difficult for programmers to migrate from one C development
environment to another. As the
need for standardization grew, the ANSI committee took on the task of defining
a single specification for the language, and ratified ANSI C in 1989.
The ISO committee ratified this
standard for international use in 1990. By the mid-1990s, it was agreed that an update to the language ought to
be considered, and the C9x project was under way. This was completed and ratified
as C99 by decade's end. (For more information on C99, visit
this online PowerPoint presentation.)
However, adoption of C99 has been relatively slow, at least relative to the strong
developer embracings of C90 and C++98. Much of this may be due to developer focus shifting away from C, and more toward
C++. Just as the 1980's was the
decade for C, the 1990's was the decade for C++. C++ was marketed to C developers as "a better C"
anyway. For this reason, some compiler vendors
simply stopped shipping separate C and C++ products, and began delivering a single C++ solution (reasoning that
C++ is essentially a superset of C anyway).
Indeed, many of the changes that came in C99, were simply adoptions of features that previously existed in C++, and which developers were already grown accustomed.
These include: C++ style // comments, intermixing declarations and statements, inline
functions, the bool type,
etc. These "new" features weren't all that new, if one was already familiar with C++. Despite this seeming convergence with C++, C99
introduced some strict incompatibilities with C++, which seemed quite disconcerting to developers. These included: the C99 complex type, and its associated clog macro. (Note: In C++, clog is an iostream global, similar to cout, whereas in C99 it is the complex logarithm
function). For this reason, C99 has not enjoyed the mindshare (or adoption rate) C90 and C++98 had. At least, thus far.
Although most C compilers today have the majority of C99
features available to use, they are not typically well advertised. Some openly question the continued need
for C in a world dominated by newer languages.
Despite this, the ISO C committee is already underway its
investigation of changes for a new version of the C language. It is called the C1x project, and it is
very early on in the process.
The first draft of the C1x spec has been released this past August.
The current plan is for C1x to be ratified in 2012, but I am inclined to think
that it may take a bit longer than that to come up with something truly compelling.
What Will Come of C++0x?
Many in the development community are concerned that C++09 will
receive only the same lukewarm response that C99 had. The parallels between C++09 and C99 are rather obvious. Both are second generation updates
coming 10 years after their primary (and extremely popular) ISO
ratifications. Both had first
generations that were ratified when each language was considered dominant in the
industry. Similarly, both have updates that are coming in a time when newer languages are beginning to take greater mindshare. With Java, C#, Ruby and others on the upswing, will C++09 offer anything
relevant? The answer to this will
come only with time.
More Information
The Wikipedia entry for C++0x is an excellent resource for those interested in pursuing this further. Also, if you'd to get direct information straight from C++'s creator, you can view this video
of Bjarne Stroustrup's presentation of C++0x to the University of Waterloo.
Coming Up Next Month: The Top 10 Mac Fiascos! See you in 30!
Other According to Hoyle columns
http://www.maccompanion.com/macc/archives/November2008/Columns/AccordingToHoyle39.htm