C++ namespaces

I know from the start that this post will shoot over the heads of probably 95% of the internet population, but of regular readers to this blog (or, those I think read reguluarly) I think they will understand what I’m getting at.

C++ is an intersting breed of programming language. Some things I like about it – like the Object Oriented (OO) stuff. Most OO concepts make sense because we think of things as objects in real life. Some things just tend to annoy me – like namespaces. I can see where they become useful in very complex code organization with short function names – eventually you will run out of names and collisions will occur. But for the most part, I think namespaces are not incredibly useful, and serve to annoy me. I spent at least an hour trying to debug code today because I was forgetting the “using namespace std” at the beginning. Having worked mostly in C code, and not using STL in a while, I just plum forgot to put it in there – annoying, but no big deal.

The frustrating part came when I wanted to use a hash_map. Now, hash_maps are included in most distributions these days, and the folks over at SGI (and probably HP) who wrote them are really smart guys. Hash_maps are associative arrays which let you index an array based on a data type other than an unsigned integer (on a side note, perl programmers use these left-and-right, upside-down and inside-out to manage data sets of any size). This is where thigns kinda get weird – the hash_map container is not a standard part of the STL – the Standard Template Library. How can you have a non-standard part of a standard? That just doesn’t make any sense!

Anyway, the “using namespace std” cleared up the error about the list I was trying to create, but not the hash_map, no they are in another namespace. But, I couldn’t get a man page for hash_maps (or any STL containers for that matter, but that’s another frustration which has been solved by purchasing a book) to tell me which namespace they are in. A quick look at the ext/hash_map file fixes that. As it turns out, hash_maps are include in the namespace __gnu_cxx. Today’s fumblings around the code didn’t make me mad, just annoyed – it’s time to dig in and refresh myself on the C, er, C++ coding.

2 Comments

  • Wyatt Neal says:

    “Use it or lose it.” is that phrase that comes to mind :-) .

    I too have fallen into this trap not once, but pretty much ever time I’ve written C++ in the past year. Given I haven’t written much other than to do some quick work for myself, but enough to remember. I do find that it’s a little odd that hash_maps aren’t included in the standard name space; however, being as I see that there is an “ext” there, I would assume that it’s really not part of the standard name space, but part of the extended name space. That’s of course my “Adam’s Razor” take on it.

  • Joe Rocklin says:

    Yes, I know – hash_maps are part of an extension to the STL by SGI – the people who are the de facto reference on the STL. They could have made the extended namespace something like stdext or extstd, not __gnu_cxx!

Leave a Reply