I can understand that boost::shared_ptr
doesn't validate for NULL
before calling a custom deleter function, but how can I achieve this? This will help me avoid writing dumb wrappers for fclose
or any function that doesn't (rightly) specify the behaviour.
My boost: #define BOOST_VERSION 104500
. This is not C++ 11 (since I use boost).
The question is related to: make shared_ptr not use delete
Sample code:
static inline
FILE* safe_fopen(const char* filename, const char* mode)
{
FILE* file = NULL;
(void)fopen_s(&file, filename, mode);
return file;
}
static inline
void safe_fclose(FILE* file)
{
if (file)
BOOST_VERIFY(0 == fclose(file));
}
...
boost::shared_ptr<FILE> file( safe_fopen(FILE_DOWNLOAD, "rb"), safe_fclose);
...
// now it is created => open it once again
file.reset( safe_fopen(FILE_DOWNLOAD, "rb"), safe_fclose);
EDIT
My question initially had a second part concerning the use of shared_ptr
: why providing the deleter as a function parameter instead of a template parameter? Apparently, the answer is here: Why does unique_ptr take two template parameters when shared_ptr only takes one? C++ 11 answer is unique_ptr, but why boost did't provide one - we'll never know.