Listing 3: Smart function pointer representation and basic construction
class function_ptr { public: function_ptr() : body(0) { } template<typename nullary_function> function_ptr(nullary_function function) : body(new adaptor<nullary_function>(function)) { } ~function_ptr() { delete body; } ... private: class callable { public: virtual ~callable() { } virtual callable *clone() const = 0; virtual void call() = 0; }; template<typename nullary_function> class adaptor : public callable { public: adaptor(nullary_function function) : adaptee(function) { } virtual callable *clone() const { return new adaptor(adaptee); } virtual void call() { adaptee(); } nullary_function adaptee; }; callable *body; };