Style Case Studies: Generic Callbacks
(Page 1 of 5 )
Part of the allure of generic code is its usability and reusability in as many kinds of situations as reasonably possible. How can the simple facility presented in the cited article be stylistically improved, and how can it be made more useful than it is and really qualify as generic and widely usable code? (This book excerpt is from
Exceptional C++ Style by Herb Sutter, ISBN 0-201-76042-8, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional.)
Editor's Note: This book presents puzzles or problems. A question is posed by JG, a term for new junior-grade military officer, and an answer is presented from a Guru.
Item 35. Generic Callbacks
JG Question
1. What qualities are desirable in designing and writing generic facilities? Explain.
Guru Question
2. The following code presents an interesting and genuinely useful idiom for wrapping callback functions. For a more detailed explanation, see the original article. [Kalev01]
Critique this code and identify:
a) Stylistic choices that could be improved to make the design better for more idiomatic C++ usage.
b) Mechanical limitations that restrict the usefulness of the facility.
template < class T, void (T::*F)() >
class callback
{
public:
callback(T& t) : object(t) {} // assign actual object to T
void execute() {(object.*F)();} // launch callback function
private:
T& object;
};
Solution
Generic Quality
1. What qualities are desirable in designing and writing generic facilities? Explain.
Generic code should above all be usable. That doesn’t mean it has to include all options up to and including the kitchen sink. What it does mean is that generic code ought to make a reasonable and balanced effort to avoid at least three things:
1. Avoid undue type restrictions. For example, are you writing a generic container? Then it’s perfectly reasonable to require that the contained type have, say, a copy constructor and a nonthrowing destructor. But what about a default constructor or an assignment operator? Many useful types that users might want to put into our container don’t have a default constructor, and if our container uses it, then we’ve eliminated such a type from being used with our container. That’s not very generic. (For a complete example, see Item 11 of Exceptional C++ [Sutter00].)
2. Avoid undue functional restrictions. If you’re writing a facility that does X and Y, what if some user wants to do Z, and Z isn’t so much different from Y? Sometimes you’ll want to make your facility flexible enough to support Z; sometimes you won’t. Part of good generic design is choosing the ways and means by which your facility can be customized or extended. That this is important in generic design should hardly be a surprise, though, because the same principle applies to object-oriented class design.
Policy-based design is one of several important techniques that allow “pluggable” behavior with generic code. For examples of policy-based design, see any of several chapters in [Alexandrescu01]; the SmartPtr and Singleton chapters are a good place to start.
This leads to a related issue:
3. Avoid unduly monolithic designs. This important issue doesn’t arise as directly in our style example under consideration below, and it deserves some dedicated consideration in its own right, hence it gets not only its own Item, but its own concluding miniseries of Items: see Items 37 through 40.
In these three points, you’ll note the recurring word “undue.” That means just what it says: Good judgment is needed when deciding where to draw the line between failing to be sufficiently generic (the “I’m sure nobody would want to use it with anything but char” syndrome) on the one hand and overengineering (the “what if someday someone wants to use this toaster-oven LED display routine to control the booster cutoff on an interplanetary spacecraft?” misguided fantasy) on the other.
This chapter is from Exceptional C++ Style, by Herb Sutter (ISBN 0201760428, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Dissecting Generic Callbacks and Improving Style >>
More Code Examples Articles
More By Addison-Wesley/Prentice Hall PTR