C++ for the next decade

C++

Although some people regard C++ as the COBOL of the 21st century, it remains a force to be reckoned with in commercial development. For example it is currently ranked fourth in the language related tags on stackoverflow.com (despite the apparent biases of Stackoverflow owners Jeff Atwood and Joel Spolsky towards web and .Net oriented languages):

Rank Language Tagged questions
1 C# 4681
2 Java 2903
3 ASP.Net 2334
4 C++ 2040
5 Javascript 1677
6 PHP 1505
7 Python 1397
8 C 851
9 Ruby 719
10 VB.Net 548

C++ has been one of the top commercial languages for a long time. But the world of computer software and hardware is changing fast and languages have to evolve to stay relevant. The first major revision to the C++ standard in a decade, known as C++ 0x, is expected be finalised in 2009 (otherwise the x will have to be hex). The draft standard includes lots of additions to the language and standard libraries, including: lambdas; closures; static (compile time) asserts; concepts; automatic types; variadic templates; a regular expressions library and improved threading support. Automatic garbage collection is notable by its absence.

For working C++ developers the new standard only really becomes important when it becomes available in mainstream compilers such as Visual C++ and Gnu C++. As Gnu C++ already has experimental support for C++ 0x and Microsoft released a preview of Visual Studio 2010 at PDC last month with support for some of new features it might be a good time to start paying attention to the new standard. You don’t want to look like an idiot next time one your colleagues starts talking about ‘lambdas’, do you?

I illustrate a few of the new features below. I have done this mainly as a way of understanding them better myself, not because I claim any sort of expertise on the new standard.

Lambdas and closures

A ‘lambda’ is an unnamed function, for example:

std::vector<int> values = getValues();
std::for_each( values.begin(), values.end(), [](int n) { cout << n << std::endl; } );

Lambdas can reference in-scope variables, for example:

std::vector<int> values = getValues();
int total = 0;
std::for_each( values.begin(), values.end(), [&total](int n) { total += n } );

This is known as a ‘closure’. Of course, all of this can be done with standard function calls, but lambdas are undoubtedly more compact and elegant. I am not convinced that they will make code any clearer or easier to debug or maintain however.

Automatic types

Automatic typing allows the compiler to infer a type. For example:

std::vector<int> values = getValues();
for ( std::vector<int>::const_iterator it = values.begin(); it != values.end(); ++it ) { f( it ); }

Can be re-written as:

auto values = getValues();
for ( auto it = values.begin(); it != values.end(); ++it ) { f( it ); }

Automatic types seem like an excellent way to let the compiler take some of the drudgery out of programming.

Concepts

Template error messages have improved over the years, but they can still be obscure. A ‘concept’ is a way to clearly define what types a templated class or function will accept. For example:

template<typename T> requires LessThanComparable<T>
const T& min(const T &x, const T &y)
{
return y < x ? y : x;
}

Specifies that min() will only accept types that support the concept LessThanComparable.  LessThanComparable can then be defined so that the type must support the < operator:

auto concept LessThanComparable<typename T>
{
bool operator<(T, T);
}

Hopefully concepts will allow better defined template interfaces and clearer error messages.

Concurrency

The current C++ standard makes no real allowances for concurrency. This is a major problem in a world where multi-core CPUs are standard. The new standard will introduce a memory model that supports threading and a standard threading library.

Summary

C++ has many strengths. It scores quite highly on performance, expressiveness and portability, has an extensive tool and library ‘ecosystem’ and a proven track record in large scale system development. But it is also a sprawling and complex language, hobbled by many shortcomings inherited from its progenitor C (a language dating back to the early seventies). It isn’t clear to me whether the new standard will add enough features to keep C++ competitive as a commercial language for another decade. Or whether C++ will be regarded as a legacy language, too bloated and complex to attract new developers given the choice of simpler and more elegant languages. Time will tell.

Further reading:

C++ 0x article on wikipedia

24 thoughts on “C++ for the next decade

  1. Sohail

    I once had lunch with Stroustrup and asked him about the closure implementation. He got very excited and said “they are not closures!”

    After that, I have always refrained from calling them closures.

    The C++0x thread library rocks! It is the best API I’ve seen for C++.

  2. noname

    > Although some people regard C++ as the COBOL of the 21st century
    I guess if any language deserves this title it is Java :-)

    If I look at the proposed C++0x standard I get a very bad feeling concerning compiler support. It took years after the release of the last c++ standard until you could use advanced stuff like A. Alexandrescu’s Loki library with more than just a few selected compilers.
    And my fear is that history will repeat with c++0x

  3. Tim

    “For example it is currently ranked fourth in the language related tags on stackoverflow.com (despite the apparent biases of Stackoverflow owners Jeff Atwood and Joel Spolsky towards web and .Net oriented languages):”

    I’m not sure what the owners’ biases have to do with it, unless they’re flooding their own website with questions, or removing non-.NET questions.

  4. Poulejapon

    To Tim :
    Stackoverflow first opened as a huge beta, open to the readers of Jeff and Joel Splosky’s blog.

    It explains the initial big bias to the user pool. Then when you think about the way people hear about stackoverflow (searching for a question on google, hearing from a friend) you understand that this bias will take a long time to disappear.

  5. Gustavo Barrancos

    I’ll pass :p. I spoke a little about it in my blog recently and i’m far from picking it for the job as a primary choice. I still have my doubts concerning the time it would take for C++0x to be properly implemented by compilers… If Scott Meyers in 2005 was still complaining of standard compliance on the newest Effective C++ , you can have an idea…

  6. Andy Brice Post author

    +1 for Poulejapon

    Jeff Atwood doesn’t know how to program in C or C++.

    Joel Spolsky has claimed that desktop software is dead.

    Surely this must have an effect on how many C++ programmers read their blogs and consequently how many have signed up for Stackoverflow?

  7. Ryan Ginstrom

    @Andy

    I consider myself mainly a python/C++ developer, and I do read stack overflow — just via the python and C++ RSS feeds

    I don’t plan on transitioning to C++0x any time soon, since I already use the boost libraries extensively and do very little greenfield development in C++ these days.

  8. Jesse

    I suppose one reason that C++ might have a disproportionate number of questions on Stack Overflow is that it’s so disproportionately complicated! I used to work with someone who was a C++ “language lawyer”, and it never failed to amaze me just how much you have to know to truly be a C++ expert.

  9. Anthony Williams

    For completeness, it’s worth mentioning that C++ Builder 2009 from CodeGear also has some C++0x features, such as rvalue references, static_assert and decltype. This is important, because it is the currently shipping version, and not just a CTP.

    Also, gcc 4.3 (which has some C++0x support) is the default compiler on Ubuntu Intrepid.

    Sohail wrote:
    > The C++0x thread library rocks! It is the best API I’ve seen for C++.

    Thanks! As one of the committee members who worked (and is still working on) the thread library API, it’s good to hear positive feedback like that.

    For those of you who use Microsoft Visual Studio 2008 (including the Express edition), my implementation of the C++0x thread library is currently in beta: http://www.stdthread.co.uk

  10. Bob Jones

    It took so long for the last C++ standard to finalize and each compiler was different (and is still) in what it implemented.

    I expect I will have to wait until 2020 until most compilers get the C++ 0x standard implemented well enough so some cross platform development is possible without crying how GCC does this, VS does that and Metrowerks does not even recognize the syntax.

  11. phil

    “Also, gcc 4.3 (which has some C++0x support) is the default compiler on Ubuntu Intrepid.”

    How much of C++0x is supported by gcc 4.3 now?

    As someone who works with C++, but plays with OCaml, Haskell and Ruby, I’m really looking forward to some of these features – especially lambdas, type inference and regexes. The concepts sounds interesting as well.

  12. Anthony Williams

    The gcc 4.3 C++0x support page is at http://gcc.gnu.org/gcc-4.3/cxx0x_status.html

    Basically, it supports rvalue references, variadic templates, decltype and static_assert, along with a few other minor bits and pieces.

    gcc 4.4 supports even more of C++0x, but it’s not released yet. See http://gcc.gnu.org/projects/cxx0x.html for an overview of gcc C++0x support across all versions.

    Regexes you can get with boost and the TR1 update for MSVC. Lambdas and type inference will have to wait. They’re in the VS10 CTP (I think), but not in any production compiler.

    Preliminary support for concepts is available in ConceptGCC (http://www.generic-programming.org/software/ConceptGCC/), but it’s not a complete implementation.

  13. Tarek Demiati

    Hello Andy,

    I understand that rewriting pefect table plan into a new framework would not make any sense, however I’m wondering if you’re thinking
    using another language for your future product.

    My main concern with C++ even with a framework such as Qt, is the productivity.
    But then unlike you, I have never use C++ commercially, and I think it does make a hell of a difference.

    Would you agree that learning C++ for creating a new product is a fast track for suicide, considering the steep learning curve before someone can become proficient in C++.

    I think that for a pure CRUD app I do find C++ being an overkill …

  14. Andy Brice Post author

    Tarek,

    I think I am very productive in C++/Qt. I know them both very well and have learnt to work around some of their shortcomings. They are my preferred choice for developing efficient, portable, native desktop apps for the foreseeable future.

    C++ does have quite a learning curve for new developers though. But you don’t have to use every feature of the language just ‘because it is there’. Also you can avoid many of the ‘gotchas’ by reading Scott Meyer’s books.

    I can’t say whether C++ is right for you. You might find this previous post interesting though:
    https://successfulsoftware.net/2008/03/24/choosing-a-development-stack-for-windows-desktop-applications/

  15. Steve

    Is for_each already in C++? If so I can’t believe I’m not using it, it has to be one of the best things about Perl.

    Also, for the love of god, the possessive “its” does not have an apostrophe!

  16. Dustin

    “it’s” v.s. “its”:
    “It’s hot in here!” (if you can change it to “it is”, then use the apostrophe)
    “Its behaviour is well-documented.” (otherwise, leave out the apostrophe)

    A similar issue appears with “who’s” v.s. “whose”:
    “Who’s going to the beach with me!?” (not me… if you can change it to “who is”, then use the apostrophe)
    “Whose workstation is this!? Porn in the workplace is against company policy!” (otherwise, use “whose” instead)

    And just because I like things that come in threes (or to be more precise, I like items in quantities of Mersenne numbers), a quick discussion of the contraction “ain’t”:
    “I ain’t going to do it.” (if you can change it to “am not”, it is used properly)
    “We ain’t going anywhere!” (otherwise, you should consider an alternative… it’s all about subject-verb agreement)

  17. Suresh

    I am really concerned about C++ future.

    The cover of “Practice of Programming” book read – simplicity, clarity and generality.

    C++ misses on at least first two of them.
    Another point of view is for any tool design there is some amount of mistake-proofing. In C++, I find there is really little.

    With this new proposed release, they are just moving further away.

    Complexity is a real monster in programming world and programmers truly concerned with performance have stuck to simplicity of C.

    Want to share one recent change: Starting with this year (may be last year) C++ is not being taught in Computer Science courses. Now the new set becomes – C, Java and C#.

Comments are closed.