Avoid: empty inline destructor definitions
Posted on 13 Jul 2008
Recently the Google C++ style guide was making the rounds on reddit.
There’s quite a lot in there that I actually disagree with, because I’m a software developer, and hey, software developers love to argue about largely pointless stuff such as this. But one thing caught my eye that I hadn’t really considered before.
Tips for designing exception classes
Posted on 28 Jun 2008
Creating good exception classes is a reasonably tricky affair. By definition, exceptions generally aren’t expected and so aren’t thrown that often. But the whole point of exceptions as opposed to error codes is that you get to choose how far up the call-stack to handle the error, rather than having to deal with every error close to the source. This means that you should aim to make your exceptions rich so the catcher is in a better position to decide if and how to handle the exceptional condition.
So here are 9 tips for designing good exception classes. I tried to make it 10, but couldn’t eek out another one!
Stop those propagating NaNs with smart references
Posted on 30 May 2008
No, this post isn’t about contraception for the elderly. Instead it’s a post on a little debugging tool I’ve found useful on the odd occasion.
In C++ (and many other languages that use similar floating point models), dividing a floating point object by 0 will yield one of three special values: inf, -inf, or nan (Not A Number).
#include <iostream>
#include <ostream>
int main()
{
using namespace std;
cout << (1.0 / 0.0) << endl; // inf
cout << (-1.0 / 0.0) << endl; // -inf
cout << (0.0 / 0.0) << endl; // nan
return 0;
}
Dividing by 0 makes no mathematical sense, and yet there’s nothing stopping us from writing it in C++ code — usually by accident, when normalizing a zero-vector for example — and so some value must be assigned to the result of such an operation.
What’s slightly horrifying is that once you have one of these special values in your code, they begin to spread. Multiplying a floating point value by nan gives you nan and it applies to the other arithmetic operators too. Most operations involving ±inf also result in ±inf, too1. In the blink of an eye your system could become flooded with meaningless garbage.
What can we do to mitigate this problem? Read the rest of this entry…
Footnotes
- except quotients such as
10 / inf, which results in0[↩]
Edit counterpart vim script
Posted on 17 May 2008
I recently decided to learn either Vim or Emacs. For no particular reason I picked Vim.
One of the things I wanted was the ability to open the header corresponding to the currently open source file (or vice versa) using a simple shortcut. So I wrote a Python script to do this for me. It’s pretty useful, so I’ve decided to share it here.
Now I can hit Ctrl-c to go to the counterpart file, or any of Ctrl-h, Ctrl-j, Ctrl-k and Ctrl-l to open the counterpart file in a split window to the left, below, above or to the right of the current window, respectively.
Counterparts are looked for in the directories “surrounding” the directory of the current file. This includes:
- the directory of the file in the current buffer
- it’s ancestors up to a specified height
- the sub-directories of those ancestors down to a specified depth
- all of the above with either
/includeor/srcappended, depending on whether the current buffer is a source or header file
Various common C and C++ file extensions are supported (.c/.h, .cpp/.hpp, .cxx/.hxx, .C/.H, .cc/.hh and so on).
In my .vimrc I have the following to plumb in the script:
python << EOF
import sys, os.path
plugin = os.path.expanduser('~/.vim/plugin')
if not plugin in sys.path: sys.path.append(plugin)
import cpp
EOF
map <C-c> :py cpp.edit_counterpart()<CR>
map <C-j> :py cpp.edit_counterpart_split('^')<CR>
map <C-k> :py cpp.edit_counterpart_split('v')<CR>
map <C-h> :py cpp.edit_counterpart_split('<')<CR>
map <C-l> :py cpp.edit_counterpart_split('>')<CR>
Download
Here’s the cpp.py module exposing the edit_counterpart() and edit_counterpart_split() functions. I’ve documented it reasonably heavily, so you should be able to adapt it to suit your needs pretty easily to change search locations, file extension associations and so on.
Enjoy!
Looking inward
Posted on 1 May 2008
You often hear people say that they would like more introspection capabilities in C++. It would certainly help when creating mock objects and such-like. On the other hand, if you think you need that kind of thing, you’re probably better off using a different language in my opinion, or at least embedding a scripting language within your application.
With that said, there are a few tricks that can actually get you quite far. Read the rest of this entry…