Implementing operator->* for Smart Pointers

来源:互联网 发布:正在配置usb端口 编辑:程序博客网 时间:2024/04/30 05:25

http://collaboration.cmc.ec.gc.ca/science/rpn/biblio/ddj/Website/articles/DDJ/1999/9910/9910b/9910b.htm#l14

 

Implementing operator->* for Smart Pointers

Dr. Dobb's Journal October 1999

Classes and member templates for smart pointers

By Scott Meyers

Scott is a C++ consultant and author of Effective C++ CD-ROM, Effective C++,and More Effective C++. You can contact him at http://www.aristeia.com/.


Partial Template Specialization andoperator->*


When I wrote More Effective C++: 35 Ways to Improve Your Programs and Designs (Addison-Wesley, 1995), one of the topics I examined was smart pointers. As a result, I get a fair number of questions about them, and one of the most interesting questions came from Andrei Alexandrescu who asked, "Shouldn't a really smart smart pointer overload operator->*? I've never seen it done." I hadn't seen it done, either, so I set out to do it. The result is instructiveand for more than justoperator->*. It also involves insights into interesting and useful applications of templates.

Review of operator->*

If you're like most programmers, you don't use operator->* on a regular basis. Consequently, before I explain how to implement this operator for smart pointers, I'll review the behavior of the built-in version.

Given a class C, a pointer pmf to a parameterless member function of C, and a pointerpc to a C object, the expression

(pc->*pmf)(); // invoke the member function *pmf on *pc

invokes the member function pointed to by pmf on the object pointed to bypc. As you can see in Listing One, pointers to member functions behave similarly to pointers to regular functions; the syntax is just a little more complicated. By the way, the parentheses aroundpc->*pmf are necessary, because the compiler would interpret

pc->*pmf(); // error!

as

pc->*(pmf()); // error!

Designing Support for operator->*

Like many operators, operator->* is binary: It takes two arguments. When implementingoperator->* for smart pointers, the left argument is a smart pointer to an object of type T. The right argument is a pointer to a member function of class T. The only thing that can be done with the result of a call tooperator->* is to hand it a parameter list for a function call, so the return type ofoperator- >* must be something to which operator() (the function call operator) may be applied.operator->*'s return value represents a pending member function call, so I'll call the type of object returned fromoperator->*, PMFC a "Pending Member Function Call."

Put all this together, and you get the pseudocode in Listing Two. Because eachPMFC object represents a pending call to the member function passed to operator->*, both the member function and PMFC::operator() expect the same list of parameters. To simplify matters, I'll assume that T's member functions never take any arguments. (I'll remove this restriction below.) That means you can refine Listing Two asListing Three.

But what is the return type of the member function pointed to by pmf? It could beint, double, or const Wombat&. It could be anything. You express this infinite set of possibilities in the usual fashionby using a template. Hence,operator->* becomes a member function template. Furthermore, PMFC becomes a template, too, because different instantiations ofoperator->* must return different types of PMFC objects. (That's because eachPMFC object must know what type to return when its operator() is invoked.)

After templatization, you can abandon pseudocode and write PMFC and SP::operator->*; see Listing Four.

Zero-Parameter Member Functions

PMFC represents a pending member function call, and needs to know two things to implement itsoperator(): the member function to call, and the object on which to invoke that member function. ThePMFC constructor is the logical place to request these arguments. Furthermore, a standard pair object seems like a logical place to store them. That suggests the implementation inListing Five.

Though it may not look it at first glance, it's all pretty simple. When creating aPMFC, you specify which member function to call and the object on which to invoke it. When you later invoke thePMFC's operator() function, it just invokes the saved member function on the saved object.

Note how operator() is implemented in terms of the built-in operator->*. BecausePMFC objects are created only when a smart pointer's user-defined operator->* is called, that means that user-definedoperator->*s are implemented in terms of the built-in operator->*. This provides nice symmetry with the behavior of the user-definedoperator-> with respect to that of the built-in operator->, because every call to a user-definedoperator-> in C++ ultimately ends in an (implicit) call to the built-in operator->. Such symmetry is reassuring. It suggests that the design is on the right track.

You may have noticed that the template parameters ObjectType, ReturnType, andMemFuncPtrType are somewhat redundant. Given MemFuncPtrType, it should be possible to figure outObjectType and ReturnType. After all, both ObjectType andReturnType are part of MemFuncPtrType. It is possible to deduceObjectType and ReturnType from MemFuncPtrType using partial template specialization, but, because support for partial specialization is not yet common in commercial compilers, I've chosen not to use that approach here. For information on a design based on partial specialization, see the accompanying text box entitled "Partial Template Specialization andoperator->*."

Given the implementation of PMFC in Listing Five,SP<T>'s operator->* almost writes itself. The PMFC object it returns demands an object pointer and a member function pointer. Smart pointers conventionally store an object pointer, and the necessary member function pointer is just the parameter passed to operator->* as in Listing Six. Consequently, the code inListing Seven should work, and for the compilers with which I tested it (Visual C++ 6 and egcs 1.1.2), it does.

Yes, I know, the code has a resource leak (the newed Wombat is neverdeleted) and it employs a using directive (using namespace std;) when using declarations will do, but please try to focus on the interaction ofSP::operator->* and PMFC instead of such relative minutiae. If you understand why the statements(pw-> *pmf)() behave the way they do, there's no doubt you can easily fix the stylistic shortcomings of this example.

By the way, because both the operator->* member functions and all thePMFC member functions are (implicitly) inline, you may hope that the generated code for the statement

(pw->*pmf)();

using SP and PMFC will be the same as the generated code for the equivalent

(pw.ptr->*pmf)();

which uses only built-in operations. The run-time cost of using SP's overloadedoperator->* and PMFC's overloaded operator() could thus be zerozero additional bytes of code, zero additional bytes of data. The actual cost, of course, depends on the optimization capabilities of your compiler as well as on your standard library's implementation of pair and make_ pair. For the two compilers (and associated libraries) with which I tested the code (after enabling full optimization), one yielded a zero-run-time-cost implementation ofoperator->*, but the other did not.

Adding Support for const Member Functions

Look closely at the formal parameter taken by SP<T>'s operator->* functions: It'sReturnType (T::*pmf)(). More specifically, it's not ReturnType (T::*pmf)() const. That means no pointer to aconst member function can be passed to operator->*, and that means thatoperator->* fails to support const member functions. Such blatantconst discrimination has no place in a well-designed software system. Fortunately, it's easy to eliminate. Simply add a secondoperator->* template to SP, one designed to work with pointers toconst member functions; see Listing Eight. Interestingly, there's no need to change anything inPMFC. Its type parameter MemFuncPtrType, will bind to any type of member function pointer, regardless of whether the function in question isconst.

Adding Support for Member Functions Taking Parameters

With the zero-parameter case under our belt, let's move on to support for pointers to member functions taking one parameter. The step is surprisingly small, because all you need to do is modify the type of the member-pointer parameter taken byoperator->*, then propagate this change through PMFC. In fact, all you really need to do is add a new template parameter tooperator->* (for the type of the parameter taken by the pointed-to member function), then update everything else to be consistent. Furthermore, becauseSP<T> should support member functions taking zero parameters as well as member functions taking one parameter, you add a newoperator->* template to the existing one. In Listing Nine, I show only support for nonconst member functions, but operator->* templates for const member functions should be available, too.

Once you've got the hang of implementing support for zero and one parameters, it's easy to add support for as many as you need. To support member functions takingn parameters, declare two member template operator->*s inside SP, one to support nonconst member functions, one to support const ones. Eachoperator->* template should take n+1 type parameters, n for the parameters, and one for the return type. Add the correspondingoperator() template to PMFC, and you're done. The source code foroperator->*s taking up to two parameters (supporting both const and nonconst member functions) is available electronically; see "Resource Center," page 5.

Packaging Support for operator->*

Many applications have several varieties of smart pointers and it would be unpleasant to have to repeat the foregoing work for each one (for an example of the different varieties of smart pointers that can be imagined, plus some killer-cool C++, see Kevin S. Van Horn's web site at http:// www.xmission.com/ ~ksvsoft/code/smart_ ptrs.html). Fortunately, support foroperator->* can be packaged in the form of a base class, as in Listing Ten.

Smart pointers that wish to offer operator->* can then just inherit fromSmartPtrBase. (This design applies only to smart pointers that contain dumb pointers to do the actual pointing. This is the most common smart pointer design, but there are alternatives. Such alternative designs may need to packageoperator->* functionality in a manner other than that described here.) However, it's probably best to use private inheritance, because the use of public inheritance would suggest the need to add a virtual destructor toSmartPtrBase, thus increasing its size (as well as the size of all derived classes). Private inheritance avoids this size penalty, though it mandates the use of a using declaration (seeListing Eleven) to make the privately inheritedoperator->* templates public. To package things even more nicely, both SmartPtrBase and the PMFC template could be put in a namespace.

Loose Ends

After I'd developed this approach to implementing operator->* for smart pointers, I posted my solution to the Usenet newsgroup comp.lang.c++.moderated to see what I'd overlooked. It wasn't long before Esa Pulkkinen made these observations:

There are at least two problems with your approach:

1. You can't use pointers to data members (though this is easy enough to solve).

2. You can't use user-defined pointers-to-members. If someone has overloaded operator->* to take objects that act like member pointers, you may want to support such "smart pointers to members" in your smart pointer class. Unfortunately, you need traits classes to get the result type of such overloadedoperator->*s.

Smart pointers to members! Yikes! Esa's right. (Actually, he's more right than I originally realized. Shortly after writing the draft of this article, one of my consulting clients showed me a problem that was naturally solved by smart pointers to members. I was surprised, too.) Fortunately, this article is long enough that I can stop here and leave ways of addressing Esa's observations in the time-honored form of exercises for the reader. So I will.

Summary

If your goal is to make your smart pointers as behaviorally compatible with built-in pointers as possible, you should supportoperator->*, just like built-in pointers do. The use of class and member templates makes it easy to implement such support, and packaging the implementation in the form of a base class facilitates its reuse by other smart pointer authors.

Acknowledgments

In addition to motivating my interest in operator->* in the first place, Andrei Alexandrescu helped me simplify my implementation ofPMFC. Andrei also provided insightful comments on earlier drafts of this paper and the accompanying source code, as did Esa Pulkkinen and Mark Rodgers. I am greatly indebted to each of them for their considerable help with this article.

DDJ

Listing One

class Wombat {          // wombats are cute Australian marsupialspublic:                 // that look something like dogs    int dig();          // return depth dug    int sleep();        // return time slept};typedef int (Wombat::*PWMF)(); // PWMF--a pointer to a Wombat member functionWombat *pw = new Wombat;  PWMF pmf = &Wombat::dig;   // make pmf point to Wombat::dig(pw->*pmf)();              // same as pw->dig();pmf = &Wombat::sleep;      // make pmf point to Wombat::sleep(pw->*pmf)();              // same as pw->sleep();

Back to Article

Listing Two

class PMFC {               // "Pending Member Function Call"public:    ...    return type operator()( parameters ) const;    ...};template<typename T>        // template for smart ptrs-to-Tclass SP {                  // supporting operator->*public:    ...    const PMFC operator->*( return type (T::*pmf)( parameters ) ) const;    ...};

Back to Article

Listing Three

class PMFC {public:    ...    return type operator()() const;    ...};template<typename T>class SP { public:   ...    const PMFC operator->*( return type (T::*pmf)() ) const;    ...};

Back to Article

Listing Four

template<typename ReturnType>   // template for a pending mbr funcclass PMFC {                    // call returning type ReturnTypepublic:    ...    ReturnType operator()() const;    ...};template<typename T>class SP { public:    ...    template<typename ReturnType>        const PMFC<ReturnType>            operator->*( ReturnType (T::*pmf)() ) const;    ...};

Back to Article

Listing Five

template<typename ObjectType,        // class offering the mem func         typename ReturnType,        // return type of the mem func         typename MemFuncPtrType>    // full signature of the mem funcclass PMFC { public:    typedef std::pair<ObjectType*, MemFuncPtrType> CallInfo;    PMFC(const CallInfo& info): callInfo(info) {}    ReturnType operator()() const         { return (callInfo.first->*callInfo.second)(); } private:    CallInfo callInfo;};

Back to Article

Listing Six

template <typename T>class SP {public:    SP(T *p): ptr(p) {}    template <typename ReturnType>        const PMFC<T, ReturnType, ReturnType (T::*)()>            operator->*(ReturnType (T::*pmf)()) const                 { return std::make_pair(ptr, pmf); }    ... private:    T* ptr;};

Back to Article

Listing Seven

#include <iostream>#include <utility>using namespace std;  template<typename ObjectType, typename ReturnType, typename MemFuncPtrType>class PMFC { ... };              // as abovetemplate <typename T>            // also as aboveclass SP { ... }; class Wombat { public:     int dig()     {        cout << "Digging..." << endl;        return 1;    }     int sleep()     {        cout << "Sleeping..." << endl;        return 5;    }};int main(){                                     // as before, PWMF is a     typedef int (Wombat::*PWMF)();    // pointer to a Wombat member function    SP<Wombat> pw = new Wombat;    PWMF pmf = &Wombat::dig;   // make pmf point to Wombat::dig     (pw->*pmf)();              // invokes our operator->*;                               // prints "Digging..."     pmf = &Wombat::sleep;      // make pmf point to Wombat::sleep     (pw->*pmf)();              // invokes our operator->*;}                              // prints "Sleeping..."

Back to Article

Listing Eight

template <typename T>class SP {public:    ...                     // as above    template <typename ReturnType>        const PMFC<T, ReturnType, ReturnType (T::*)() const>  // const added            operator->*(ReturnType (T::*pmf)() const) const   // const added                { return std::make_pair(ptr, pmf); }    ...                     // as above};

Back to Article

Listing Nine

template <typename ObjectType, typename ReturnType, typename MemFuncPtrType>class PMFC {public:    typedef pair<ObjectType*, MemFuncPtrType> CallInfo;    PMFC(const CallInfo& info)   : callInfo(info) {}    // support for 0 parameters    ReturnType operator()() const         { return (callInfo.first->*callInfo.sd)(); }   support for 1 parameter    template <typename Param1Type>        ReturnType operator()(Param1Type p1) const            { return (callInfo.first->*callInfo.second)(p1); }private:    CallInfo callInfo;}; template <typename T>class SP {public:    SP(T *p): ptr(p) {}     // support for 0 parameters    template <typename ReturnType>        const PMFC<T, ReturnType, ReturnType (T::*)()>             operator->*(ReturnType (T::*pmf)()) const                { return std::make_pair(ptr, pmf); }    // support for 1 parameter    template <      typename ReturnType, typename Param1Type>        const PMFC<T, ReturnType, ReturnType (T::*)(Param1Type)>            operator->*(ReturnType (T::*pmf)(Param1Type)) const                { return std::make_pair(ptr, pmf); }    ... private:    T* ptr;};

Back to Article

Listing Ten

template <typename T>   // base class for smart pointers wishingclass SmartPtrBase {    // to support operator->*public:    SmartPtrBase(T *initVal): ptr(initVal) {}    // support for 0 parameters    template <typename ReturnType>        const PMFC<T, ReturnType, ReturnType (T::*)()>             operator->*(ReturnType (T::*pmf)()) const                { return std::make_pair(ptr, pmf); }    // support for 1 parameter    template <   typename ReturnType, typename Param1Type>        const PMFC<T, ReturnType, ReturnType (T::*)(Param1Type)>            operator->*(ReturnType (T::*pmf)(Param1Type)) const                { return make_pair(ptr, pmf); }    ...protected:    T* ptr;};

Back to Article

Listing Eleven

template <typename T>class SP: private SmartPtrBase<T> {public:    SP(T *p ): SmartPtrBase<T>(p) {}    using SmartPtrBase<T>::operator->*;    // make the privately inherited                                           // operator->* templates public    // normal smart pointer functions would go here;  operator->*    // functionality is inherited};

Back to Article

Listing Twelve

template <typename T>                       // traits classstruct MemFuncTraits {};template <typename R, typename O>           // partial specializationstruct MemFuncTraits<R (O::*)()> {          // for zero-parameter    typedef R ReturnType;                   // non-const member    typedef O ObjectType;                   // functions};template <typename R, typename O>           // partial specializationstruct MemFuncTraits<R (O::*)() const> {    // for zero-parameter    typedef R ReturnType;                   // const member    typedef O ObjectType;                   // functions};template <typename R, typename O, typename P1>  // partial specializationstruct MemFuncTraits<R (O::*)(P1)> {            // for one-parameter    typedef R ReturnType;                       // non-const member    typedef O ObjectType;                       // functions};template <typename R, typename O, typename P1> // partial specializationstruct MemFuncTraits<R (O::*)(P1) const> {     // for one-parameter    typedef R ReturnType;                      // const member    typedef O ObjectType;                      // functions};

Back to Article

Listing Thirteen

template <typename MemFuncPtrType>class PMFC {public:    typedef typename MemFuncTraits<MemFuncPtrType>::ObjectType ObjectType;    typedef typename MemFuncTraits<MemFuncPtrType>::ReturnType ReturnType;    ...                 // same as before};

Back to Article

Listing Fourteen

template <typename MemFuncPtrType>const PMFC<MemFuncPtrType>operator->*(MemFuncPtrType pmf) const{ return std::make_pair(ptr, pmf); }

Back to Article

原创粉丝点击