C++0x highlights #0: Range Based For Loop


Herb Sutter has some good news, C++0x looks like it could end up as C++11! This is going to be a great update to the C++ language. Lots of advanced features, like closures, a new memory model, portable threading support etc., are coming, but I think the one that I miss most often is the really simple Range Based For Loop (especially combined with Type Inference).

The following for loop:

map<SomeClass, vector> someclass_strings_map;
for (map<SomeClass, vector>::const_iterator it = someclass_strings_map.begin();
    it != someclass_strings_map.end(); ++it) {
    do_something_with(it->second);
}

is one of the reasons the Python, Ruby (and Java since 1.5) people laugh at us. But next year, we will be able to do:

for (auto x : foo_strings_map) {
        do_something_with(x.second);
}

PS: while I was compiling this example, I remembered another tiny improvement, you can now do map<Foo, vector<string>>. Notice the missing space? C++ no longer confuses nested templates with the shift/stream operator. Currently, you need an extra space: map<Foo, vector<string> >

PPS: If do_something_with() is a simple function this could be done with a for_each and a lambda/closure, but that is another story.

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter.

XP Meetup: 97 Things Every Programmer Should Know


I know this is cheating, I am supposed to blog on Fridays, but I am really excited about Monday’s XP Meetup. Kevlin Henney is going to present his new book 97 Things Every Programmer Should Know: Collective Wisdom from the Experts, together with some of the Scandinavian contributors.

I have seen Kevlin in a couple of courses at work, and also a couple of times at JavaZone. He is both knowledgeable, fun and engaging, so this should be a great night.

This is happening at Radisson Blu Scandinavia Hotel in Oslo, Monday 22 March at 6:30 PM, and there are still 14 more free seats! If you want to attend, join Oslo XP Meetup on meetup.com.

Quick Tip: Ensuring proper indentation in vim


Quick tip: You can add vim configuration options in source code comments, and vim will load them when it loads the file. For instance, I tend to put
// vim: et ts=4
at the top of my source files, to ensure that tabs are always expanded to four spaces.

The returning function that never returned


Investigating a crash report yesterday, I came across this piece of code (simplified for the purpose of this post):

std::string foo() try {
    bar();
    return "foo";
} catch (...) {
    log("Unable to foo!")
}

int main() {
    std::string s = foo();
}

Today’s exercise: what happens if bar() throws? Obviously, the function doesn’t return, since the exception is thrown and control is passed to the catch block. But since this block does not return, and does not rethrow, foo() neither returns nor throws, so what goes into the string s? Answer: No one knows, we are left in the happy land called undefined behaviour.

The morale? If the original implementor had used -Wall, he would have gotten a warning that execution reaches the end of the non-void function. (Both Sun Studio and gcc happlily compiles without complaining otherwise.)

Update:I have posted a follow up going into more details on function try blocks.

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter

New Blog!


Welcome to my new blog! Having programmed various languages since ~1997, I have worked as a C++-programmer for a year now, and find it a fascinating language. In this blog I will post anything C++-related I come across and find interesting, in the hope that someone else will find it useful as well.

Posted in Meta. Tags: . Leave a Comment »