
On 11/2/07, Mike Tegtmeyer <tegtmeye@eecis.udel.edu> wrote:
I am often asked why shared_ptr always uses the custom deleter even when the pointer argument is zero. I know it is safe to delete a NULL pointer so this works with delete but it would seem that if the user is giving a custom deleter, then they are probably doing something else. ie
//will crash if fread returns 0 shared_ptr<FILE> foo(fread("foo.txt","r"),fclose);
This is an easy screw-up for most new folks and the docs don't really explicitly point out this (I think) non-obvious behavior.
One can obviously wrap the deleter with a check among numerous other things but I'm curious what the rationale was since this won't work "out of the box" with C APIs. Can someone point me to the dialog discussing the design decision for this behavior so I can pass it along?
I don't think shared_ptr is designed to work on things like a FILE*. One uses filestream or writes a simple class wrapping up the FILE*.