Boost logo

Boost :

From: Kevin Atkinson (kevinatk_at_[hidden])
Date: 2000-03-29 00:31:17


On Tue, 28 Mar 2000, Ian Bruntlett wrote:

> 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.
>
> // 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);
> }

So you are adding specializations for a particular resources. But what is a
resource, is it the type you would normally pass in with auto_ptr?

Also, if you want to define a custom destructor you still have to write
code to specialize it. However, in my view, you don't really save much
because you still have to write about as much code as you would if you
defined a special class to point in.

Also, what if you want to handle the same resource in two different ways?

---
Kevin Atkinson
kevinatk_at_[hidden]
http://metalab.unc.edu/kevina/

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