Re: delete in-place corresponding to placement new?
From: Rob Williscroft (rtw_at_freenet.REMOVE.co.uk)
Date: 02/29/04
- Next message: Jonathan Turkanis: "Re: Class member method"
- Previous message: MacFly: "Class member method"
- In reply to: Peter Olcott: "delete in-place corresponding to placement new?"
- Next in thread: Peter Olcott: "Re: delete in-place corresponding to placement new?"
- Reply: Peter Olcott: "Re: delete in-place corresponding to placement new?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Feb 2004 22:13:33 GMT
Peter Olcott wrote in
news:OFs0c.119804$hR.2308785@bgtnsc05-news.ops.worldnet.att.net:
> I have just built a class that provides the most useful subset of
> std::vector functionality for use by compilers that lack template
> capability.
>
> http://home.att.net/~olcott/std_vect.html
>
> Through suggestions from this news group I was able to exactly
> duplicate the interface of std::vector, except for one aspect. What I
> need is a way to invoke the destructor on elements of an array without
> de-allocating the memory of this array. This would correspond to
> placement new, constructing without allocating. The most obvious way
> that comes to mind is to merely explicitly invoke the destructor. This
> capability is not available on a circa 1990 compiler. Does anyone have
> any good ideas?
>
Assuming that you can't do:
template < typename T >
inline void destroy( T *t )
{
t->~T();
}
Then you could try:
#define OFFSETOFF( Class, Member ) \
(((char *)&((Class *)0)->Member) - ((char *)0))
template < class T >
struct cheat_detor
{
T item;
inline void operator delete (void *) {}
static void destroy( T *ptr )
{
delete (
(cheat_detor< T > *)
(((char *)ptr) - OFFSETOFF( cheat_detor< T >, item ))
);
}
};
template < class T >
inline void destroy( T * ptr )
{
cheat_detor< T >::destroy( ptr );
}
The above is non-standard code, infact g++ will issue a warning
if T is a non-POD type say:
struct X
{
X() {}
~X() {}
};
Rob.
-- http://www.victim-prime.dsl.pipex.com/
- Next message: Jonathan Turkanis: "Re: Class member method"
- Previous message: MacFly: "Class member method"
- In reply to: Peter Olcott: "delete in-place corresponding to placement new?"
- Next in thread: Peter Olcott: "Re: delete in-place corresponding to placement new?"
- Reply: Peter Olcott: "Re: delete in-place corresponding to placement new?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|