Monday, March 31, 2008

Snowboarding!

More snowboarding videos!
This time its Chris shooting the video with me (in yellow) going down the mountain.
The video was edited using iMovie, which is a nifty little program, if you ignore the really obvious bugs.
Enjoy!

Friday, March 21, 2008

Koenig lookup in C++

This is a nerdcentral post, so non-nerds (all two of you) can stop reading now.

I was dealing with some piece of code at work which looked like this:


// In some header file
namespace N {
class C {
...
};

void DoStuff(const C& c_obj) { ... }
} // namespace N

// In some .cc file
N::C c_obj;
...
DoStuff(c); // <-- Look! No need for N::DoStuff


I looked at DoStuff(c) without the N:: prefix and figured that it must be a bug. Someone forgot to put it in. But this code compiles fine - what gives?

I remember reading about this way back when I was really into C++ and thought it was the hottest stuff on the planet. Thankfully those days are behind me, but not everything that I learnt. So I dug into this a bit and finally came across Herb Sutter's article explaining exactly this.

Before you read the article or even read this one furthur, its a good exercise to try and figure out what's going on. What are the possibilities that would make this work? A good guess is someone did a
using namespace N;
somewhere up above. This would indeed make things work and this is why using namespace declarations are a very, very bad idea in header files. They're usually a bad idea in .cc files too, but less so than headers.

However, the above example isn't because of a using namespace somewhere up above. Instead, it is because of a feature of C++ called Koenig lookup. The basic idea is that besides the usual places where a compiler looks to resolve a symbol (local scope, global scope, etc) it must also look in the namespaces containing each of the parameters of the function. As a result, our good ol' compiler looks within namespace N as well, and lo and behold, there's DoStuff defined in N.

Why is this useful?
STL uses this feature quite a bit. Its "normal" to declare things like this:


namespace stl {
template
class datastructure {...}
}
} // namespace stl

template
void operator+(stl::datastructure& s1, const stl::datastructure& s2)


I'm not certain why this is better than declaring operator+ to be a part of datastructure itself (it will have the same effect), but this is C++ arcane voodoo that is beyond me. Anyway, I prefer using better tools these days, but now and then, we have to deal with languages that were really not intended for the ordinary mortal programmer.

Monday, March 10, 2008

Ted's mornings

This is my model of what Ted's mornings are like. Much cleaner room, of course, etc, but you get the point.

The Cult of Obama

It started with this twitter from obama's campaign:
"BarackObama: In Columbus,MS & wondering how somebody who's in second place is offering the vice presidency to the person who's in first place. Vote Tues!"
Hil-a-rious!
All things lead to YouTube and this message was no exception. In the links that followed on the reply twitters, I came across this video:

I'm a die hard obama supporter. I think the guy is awesome in many, many ways.
But that video freaked me out. That video is so singularly cult like that its not even funny. This follows on the heels of a piece I heard on NPR (podcasts) where the Wait, Wait folks make fun of Obama comparing him to Scientologists and got hordes of angry letters from the Obama supporters. Kind of like when they made fun of Scientologists and got hordes of angry letters from the Scientologists.

Edit: Kristin sent me this over IM:

Translation:
To the candidate who is Barack Obama
I sing this corrido with all my soul
He was born humble without pretension
He began in the streets of Chicago
Working to achieve a vision
To protect the working people
And bring us all together in this great nation
Viva Obama! Viva Obama!
Families united and safe and even with a health care plan
Viva Obama! Viva Obama!
A candidate fighting for our nation
It doesn't matter if you're from San Antonio
It doesn't matter if you're from Corpus Christi
From Dallas, from the Valley, from Houston or from El Paso
What matters is that we vote for Obama
Because his struggle is also our struggle, and today we urgently need a change
Let's unite with our great friend
Viva Obama! Viva Obama!
Families united and safe and even with a health care plan
Viva Obama! Viva Obama!
A candidate fighting for our nation

My take: OMG.