8 June 2005

Generalized functors

Required reading for C++ programmers: Alexandrescu's Modern C++ Design. I've been re-reading his chapter on generalized functors in the hope of seeing how I could have improved my recent code with abstracted database access. I wrapped specific MFC database actions in functions or function objects that are passed to a main function for execution. The main function catches the different MFC errors and checks return codes, then converts them into a custom exception hierarchy. This simplified the main code by eliminating multiple exception handlers around the code and multiple error checks inside the code. However, my function objects had limited versatility because their signatures had to stay the same.

Alexandrescu's generalized functors would have been ideal. He has created a small library that can wrap every type of callable entity (C functions, C function pointers, references to functions, functors, and member functions), along with parameter binding, in a single template. Very nice.

[ posted by sstrader on 8 June 2005 at 12:28:48 PM in Programming ]