Theoretically, I should be able to use my own type of pointer and delete it in order to have a unique_ptrcontrol object that is not a pointer. I tried the following code:
#ifndef UNIQUE_FD_H
#define UNIQUE_FD_H
#include <memory>
#include <unistd.h>
struct unique_fd_deleter {
typedef int pointer;
void operator()( int fd )
{
close(fd);
}
};
typedef std::unique_ptr<int, unique_fd_deleter> unique_fd;
#endif
This does not work (gcc 4.7 with option -std=c++11). It responds with the following errors:
In file included from /usr/include/c++/4.7/memory:86:0,
from test.cc:6:
/usr/include/c++/4.7/bits/unique_ptr.h: In instantiation of 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = int; _Dp = unique_fd_deleter]':
test.cc:22:55: required from here
/usr/include/c++/4.7/bits/unique_ptr.h:172:2: error: invalid operands of types 'int' and 'std::nullptr_t' to binary 'operator!='
unique_ptr, , . , , , unique_ptr "" (, , int) nullptr, , . , , "pointer()" ( ""). , - nullptr.
, - unique_ptr . , :
unique_fd fd( open(something...) );
if( !fd )
throw errno_exception("Open failed");
, , unique_ptr , " " -1, .
gcc, - , ?