Boost logo

Boost :

From: Ian Bruntlett (Ian.Bruntlett_at_[hidden])
Date: 2000-03-28 05:17:44


Hi,

Can someone help with template specialisation of operator~()
Source code is at the bottom of the message.

Kevin Atkinson <kevinatk_at_[hidden]> wrote...
> Well how the heck do you manage to pull that off withot pasing in a
> function class, function, or something like that.
Simple idea : declare (but don't define) operator~ for auto_resource in the
template.
I couldn't get that to work. So I defined a member function ReleaseResource
which gets called by the destructor.

> After reading it again I take it you want auto_ptr to inherit a class.
> But in you want how it destroys the object you will STILL have to have an
> extra class. So you really did not save anything.
It is conceptual inheritance only. That's why I called it a "generic
ancestor" in an attempt to distinguish it from O.O. class inheritance.

To test my idea further, I've knocked up some basic code for auto_resource.
To keep things *really* simple, I decided to manage a resource of type
"int".
OK, I admit, ints don't need resource management. But the idea can be
applied to things that do.

TIA,

Ian

// in a header...
template <typename Resource_t>
class auto_resource
{ // yes, I know it doesn't implement all of auto_ptr - let's get
destruction dealt with before dealing with the rest of it.
  public:
    auto_resource(Resource_t ResourceToGuard) : Resource(ResourceToGuard)
{};
    ~auto_resource() { ReleaseResource(Resource); }
  private:
    void ReleaseResource(Resource_t &ResourceToRelease);
    Resource_t Resource;
};

/* Couldn't get template specialisation of an operator to work
template <> auto_resource<int>::operator~()
{ // "int"s aren't resources - this is for testing purposes only
  Resource=6;
}
*/

// in user code...

template <> void auto_resource<int>::ReleaseResource(int &ResourceToRelease)
{
  Resource=2;
}

static void TestAutoResource(void)
{
  auto_resource<int> TestResource(10);
}


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