Boost logo

Boost :

Subject: [boost] [Gauge for interest] Surrogate template
From: Alan sinde (alantsd_at_[hidden])
Date: 2011-02-27 01:56:16


Hi all,

Recently I've an surrogate/proxy pattern implemented for storing related type as value.
It is based on chapter 5 of Rumination on C++ by Andrew Koenig and Barbara moo.
Original version required stored object to define their own virtual copy constructor.

I have updated it to template version.

Besides, it also relief user from defining virtual copy constructor by moving virtual copy inside surrogate. Thanks to template constructor making this possible.
 
Drawback:
1) Each copy construction of surrogate incurs 2 virtual calls instead of 1 in the original.
2) Object compositing surrogate that has interface taking derived class must change to template member function in order to use template constructor of surrogate.

Does anybody interested to include it as boost::surrogate?

Example Usage:

class boy
{
public:
    virtual void tell() = 0;
};

class happy_boy : public boy
{
    ...
};
class study_boy : public boy
{
    ...
};

void say_hi(boy& the_boy)
{
    the_boy.tell();
}

int main()
{
    std::cout << "Hello boost::surrogate!" << std::endl;
    happy_boy Terry;
    study_boy Thomas;

    std::vector<boost::surrogate<boy> > boy_list;
    boy_list.push_back(boost::surrogate<boy>(Terry));
    boy_list.push_back(boost::surrogate<boy>(Thomas));

    std::for_each(boy_list.begin(), boy_list.end(), say_hi);

    std::cin.get();
}

Regards,
Alan Tong
                                               


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk