boost/checked_delete.hpp

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

boost/checked_delete.hpp

mbiddeg
Hi Guys.
I need serious help here, though this is child's play for you all.
>From boost/checked_delete.hpp (attached),

template<class T> inline void checked_delete(T * x)
{
    // intentionally complex - simplification causes regressions
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete x;
}

template<class T> inline void checked_array_delete(T * x)
{
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete [] x;
}


*         What's the result of applying sizeof to an incomplete type?

*         Why is sizeof called twice?

*         Why is the result of sizeof cast to void? What exactly does that line do?

*         Is it an empty statement if the sizeof is legal?

*         Why not apply x = 0; at the end?

This code is not too clear to me & the boost documentation is not either. Please assist. Please do not direct me to the C++ standard text.
Then again, smart pointers are simple to understand & use, but their implementations are all rather difficult to understand (maybe except scoped_ptr). Which brings me to one more question, why wasn't Andrei's technique of Smart_Ptr (Loki) chosen over the boost implementation technique (a class per smart pointer) for C++ 0x? His allows for an innumerable number of extension through policies. I ask this purely to understand why, not as a rejoinder.
Awaiting your feedback.
Thank you.
Nice day.


______________________________________________________________________

Kizza George Mbidde
Interconnect Billing Systems Analyst
IT
cell: +256 77 221 2982
email: [hidden email]
[cid:image001.gif@01C9E853.125FFD20]


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

image001.gif (7K) Download Attachment
checked_delete.hpp (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: boost/checked_delete.hpp

Peter Dimov-5
Kizza George Mbidde:
...
>    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
>    (void) sizeof(type_must_be_complete);
>    delete x;

...

> * What's the result of applying sizeof to an incomplete type?

A compile-time error, unless the compiler chooses to return 0 as a
nonstandard extension.

> * Why is sizeof called twice?

The second sizeof is a workaround for a Metrowerks CodeWarrior bug in which
the first typeof is never instantiated unless used.

> * Why is the result of sizeof cast to void? What exactly does that line
> do?

Silences a compiler warning.

> * Is it an empty statement if the sizeof is legal?

Yes.

> * Why not apply x = 0; at the end?

It wouldn't do anything as x is passed by value.

--
Peter Dimov
http://www.pdplayer.com 

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: boost/checked_delete.hpp

mbiddeg
Thanks Peter.

This clears things up for me.

However, concerning the last point x is passed by reference (pointer), as in inline void checked_delete(T * x)

Thank you once again.



______________________________________________________________________



Kizza George Mbidde

Interconnect Billing Systems Analyst

IT

cell: +256 77 221 2982

email: [hidden email]







-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Peter Dimov
Sent: Monday, June 08, 2009 16:52
To: [hidden email]
Subject: Re: [boost] boost/checked_delete.hpp



Kizza George Mbidde:

...

>    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];

>    (void) sizeof(type_must_be_complete);

>    delete x;



...



> * What's the result of applying sizeof to an incomplete type?



A compile-time error, unless the compiler chooses to return 0 as a

nonstandard extension.



> * Why is sizeof called twice?



The second sizeof is a workaround for a Metrowerks CodeWarrior bug in which

the first typeof is never instantiated unless used.



> * Why is the result of sizeof cast to void? What exactly does that line

> do?



Silences a compiler warning.



> * Is it an empty statement if the sizeof is legal?



Yes.



> * Why not apply x = 0; at the end?



It wouldn't do anything as x is passed by value.



--

Peter Dimov

http://www.pdplayer.com



_______________________________________________

Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: boost/checked_delete.hpp

Gennadiy Rozental-2
 <mbiddeg <at> mtn.co.ug> writes:

>
> Thanks Peter.
>
> This clears things up for me.
>
> However, concerning the last point x is passed by reference (pointer), as in
inline void checked_delete(T
> * x)

I'm sorry, but you are wrong. x is pointer passed by value.

Gennadiy

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost