Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

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:

  1. 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.

Dissecting Generic Callbacks

  1. The following code presents an interesting and genuinely useful idiom for wrapping callback functions. For a more detailed explanation, see the original article. [Kalev01]

Here again is the code:

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;
   };
   

Now, really, how many ways are there to go wrong in a simple class with just two one-liner member functions? Well, as it turns out, its extreme simplicity is part of the problem. This class template doesn't need to be heavyweight, not at all, but it could stand to be a little less lightweight.

Improving Style

  • Critique this code and identify:

    1. Stylistic choices that could be improved to make the design better for more idiomatic C++ usage.

How many did you spot? Here's what I came up with:

  1. The constructor should be explicit. The author probably didn't mean to provide an implicit conversion from T to callback<T>. Well-behaved classes avoid creating the potential for such problems for their users. So what we really want is more like this:

    explicit callback(T& t) : object(t) {} // assign actual object to T
             

While we're already looking at this particular line, there's another stylistic issue that's not about the design per se but about the description:

Guideline

Prefer making constructors explicit unless you really intend to enable type conversions.

(Nit) The comment is wrong. The word "assign" in the comment is incorrect and so somewhat misleading. More correctly, in the constructor, we're "binding" a T object to the reference and by extension to the callback object. Also, after many rereadings I'm still not sure what the "to T" part means. So better still would be "bind actual object."

explicit callback(T& t) : object(t) {} // bind actual object
   

But then, all that comment is saying is what the code already says, which is faintly ridiculous and a stellar example of a useless comment, so best of all would be:

explicit callback(T& t) : object(t) {}
  1. The execute function should be const. The execute function isn't doing anything to the callback<T> object's state, after all! This is a "back to basics" issue: Const correctness might be an oldie, but it's a goodie. The value of const correctness has been known in C and C++ since at least the early 1980s, and that value didn't just evaporate when we clicked over to the new millennium and started writing lots of templates.

void execute() const { (object.*F)(); }       // launch callback function
   

Guideline

Be const correct.

While we're already beating on the poor execute function, there's an arguably more serious idiomatic problem:

  1. (Idiom) And the execute function should be spelled operator(). In C++, it's idiomatic to use the function-call operator for executing a function-style operation. Indeed, then the comment, already somewhat redundant, becomes completely so and can be removed without harm because now our code is already idiomatically commenting itself. To wit:

    void operator()() const { (object.*F)(); } // launch callback function
             

"But," you might be wondering, "if we provide the function-call operator, isn't this some kind of function object?" That's an excellent point, which leads us to observe that, as a function object, maybe callback instances ought to be adaptable too.

Guideline

Provide operator() for idiomatic function objects rather than providing a named execute function.

Pitfall: (Idiom) Should this callback be derived from std::unary_function? See Item 36 in [Meyers01] for a more detailed discussion about adaptability and why it's a Good Thing in general. Alas, here, there are two excellent reasons why callback should not be derived from std::unary_function, at least not yet:

  • It's not a unary function. It takes no parameter, and unary functions take a parameter. (No, void doesn't count.)

  • Deriving from std::unary_function isn't going to be extensible anyway. Later on, we're going to see that callback perhaps ought to work with other kinds of function signatures too, and depending on the number of parameters involved, there might well be no standard base class to derive from. For example, if we supported callback functions with three parameters, we have no std::ternary_function to derive from.

Deriving from std::unary_function or std::binary_function is a convenient way to give callback a handful of important typedefs that binders and similar facilities often rely upon, but it matters only if you're going to use the function objects with those facilities. Because of the nature of these callbacks and how they're intended to be used, it's unlikely that this will be needed. (If in the future it turns out that they ought to be usable this way for the common one-and two-parameter cases, then the one-and two-parameter versions we'll mention later can be derived from std::unary_function and std::binary_function, respectively.)

Correcting Mechanical Errors and Limitations

  1. Mechanical limitations that restrict the usefulness of the facility.

  1. Consider making the callback function a normal parameter, not a template parameter. Non-type template parameters are rare in part because there's rarely much benefit in so strictly fixing a type at compile time. That is, we could instead have:

    template < class T >
    class callback {
    public:
     typedef void (T::*Func)();
             
             callback(T& t, Func func) : object(t), f(func) {}      // bind  actual object
             void operator()() const { (object.*f)(); }             // launch callback function
             
             private:
             T& object;
             Func f;
             };
             

Now the function to be used can vary at run-time, and it would be simple to add a member function that allowed the user to change the function that an existing call-back object was bound to, something not possible in previous versions of the code.

Guideline

It's usually a good idea to prefer making non-type parameters into normal function parameters, unless they really need to be template parameters.

  1. Enable containerization. If a program wants to keep one callback object for later use, it's likely to want to keep more of them. What if it wants to put the callback objects into a container, such as a vector or a list? Currently that's not possible, because call-back objects aren't assignable—they don't support operator=. Why not? Because they contain a reference, and once that reference is bound during construction, it can never be rebound to something else.

Pointers, however, have no such compunction and are quite happy to point at what-ever you'd ask them to. In this case it's perfectly safe for callback instead to store a pointer, not a reference, to the object it's to be called on and then to use the default compiler-generated copy constructor and copy assignment operator:

template < class T >
class callback {
public:
 typedef void (T::*Func)();

 callback(T& t, Func func) : object(&t), f(func) {}      // bind actual object
   void operator()() const { (object->*f)(); }             // launch callback function
   
   private:
   T* object;
   Func f;
   };
   

Now it's possible to have, for example, a list< callback< Widget, &Widget::Some-Func > >.

Guideline

Prefer to make your objects compatible with containers. In particular, to be put into a standard container, an object must be assignable.

"But wait," you might wonder at this point, "if I could have that kind of a list, why couldn't I have a list of arbitrary kinds of callbacks of various types, so that I can remember them all and go execute them all when I want to?" Indeed, you can, if you add a base class:

  1. Enable polymorphism: Provide a common base class for callback types. If we want to let users have a list<callbackbase*> (or, better, a list< shared_ptr<callbackbase> >) we can do it by providing just such a base class, which by default happens to do nothing in its operator():

    class callbackbase {
             public:
             virtual void operator()() const { };
             virtual ~callbackbase() = 0;
             };
             
             callbackbase::~callbackbase() { }
             
             template < class T >
             class callback : public callbackbase {
             public:
             typedef void (T::*Func)();
             
             callback(T& t, Func func) : object(&t), f(func) {}       // bind actual object
             void operator()() const { (object->*f)(); }              // launch callback function
             
             private:
             T* object;
             Func f;
             };
             

Now anyone who wants to can keep a list<callbackbase*> and polymorphically invoke operator() on its elements. Of course, a list< shared_ptr<callback> > would be even better; see [Sutter02b].

Note that adding a base class is a tradeoff, but only a small one: We've added the overhead of a second indirection, namely a virtual function call, when the callback is triggered through the base interface. But that overhead actually manifests only when you use the base interface. Code that doesn't need the base interface doesn't pay for it.

Guideline

Consider enabling polymorphism so that different instantiations of your class template can be used interchangeably, if that makes sense for your class template. If it does, do it by providing a common base class shared by all instantiations of the class template.

  1. (Idiom, Tradeoff) There could be a helper make_callback function to aid in type deduction. After a while, users may get tired of explicitly specifying template parameters for temporary objects:

    list< callback< Widget > > l;
    l.push_back(callback<Widget>(w, &Widget::SomeFunc));
             

Why write Widget twice? Doesn't the compiler know? Well, no, it doesn't, but we can help it to know in contexts where only a temporary object like this is needed. Instead, we could provide a helper so that they need only type:

list< callback< Widget > > l;
l.push_back(make_callback(w, &Widget::SomeFunc));
   

This make_callback works just like the standard std::make_pair. The missing make_callback helper should be a function template, because that's the only kind of template for which a compiler can deduce types. Here's what the helper looks like:

template<typename T >
callback<T> make_callback(T& t, void (T::*f) ()) {
 return callback<T>(t, f);
}
  1. (Tradeoff) Add support for other callback signatures. I've left the biggest job for last. As the Bard might have put it, "There are more function signatures in heaven and earth, Horatio, than are dreamt of in your void (T::*F) ()!"

Guideline

Avoid limiting your template; avoid hardcoding for specific types or for less general types.

If enforcing that signature for callback functions is sufficient, then by all means stop right there. There's no sense in complicating a design if we don't need to—for complicate it we will, if we want to allow for more function signatures!

I won't write out all the code, because it's significantly tedious. (If you really want to see code this repetitive, or you're having trouble with insomnia, see books and articles like [Alexandrescu01] for similar examples.) What I will do is briefly sketch the main things you'd have to support and how you'd have to support them:

First, what about const member functions? The easiest way to deal with this one is to provide a parallel callback that uses the const signature type, and in that version, remember to take and hold the T by reference or pointer to const.

Second, what about non-void return types? The simplest way to allow the return type to vary is by adding another template parameter.

Third, what about callback functions that take parameters? Again, add template parameters, remember to add parallel function parameters to operator(), and stir well.

Remember to add a new template to handle each potential number of callback arguments.

Alas, the code explodes, and you have to do things like set artificial limits on the number of function parameters that callback supports. Perhaps in a future C++0x language we'll have features like template "varargs" that will help to deal with this, but not today.

Summary

Putting it all together, and making some purely stylistic adjustments like using typename consistently and naming conventions and whitespace conventions that I happen to like better, here's what we get:

class CallbackBase {
public:
 virtual void operator()() const { };
 virtual ~CallbackBase() = 0;
};

CallbackBase::~CallbackBase() { }

template<typename T>
class Callback : public CallbackBase {
public:
 typedef void (T::*F)();

 Callback(T& t, F f) : t_(&t), f_(f) { }
 void operator()() const { (t_->*f_)(); }

private:
 T* t_;
 F f_;
};

template<typename T>
Callback<T> make_callback(T& t, void (T::*f) ()) {
 return Callback<T>(t, f);
}
  • + Share This
  • 🔖 Save To Your Account