vendredi 3 avril 2015

unique_ptr with custom pointer type: *get() and operator*() give different output

According to cppreference calling std::unique_ptr::operator*() is equivalent to calling *(std::unique_ptr::get()).


However I'm getting different results for both calls. Here is my code:



#include <iostream>
#include <string>
#include <memory>

#include <fcntl.h>
#include <unistd.h>

struct file_descriptor
{
private:
struct closer;

public:
typedef int handle_type;
typedef closer closer_type;

constexpr static handle_type kInvalidHandle = -1;

public:
file_descriptor(int handle = kInvalidHandle) : handle_{ handle } { }
file_descriptor(std::nullptr_t) : file_descriptor{ } { }

operator int&() { return handle_; }
operator int() const { return handle_; }

int& operator*() { return static_cast<int&>(*this); }
int operator*() const { return static_cast<int>(*this); }

bool operator==(const file_descriptor& other) const
{ return (handle_ == other.handle_); }

bool operator!=(const file_descriptor& other) const
{ return !(*this == other); }

private:
struct closer
{
typedef file_descriptor pointer;

void operator()(pointer handle) const
{ ::close(*handle); }
};

int handle_;
};

using unique_file_ptr = std::unique_ptr<typename file_descriptor::handle_type,
typename file_descriptor::closer_type>;

unique_file_ptr managed_open(const std::string& path)
{
return { ::open(path.c_str(), O_RDWR), { } };
}

int main(int, char**)
{
auto handle = managed_open("/dev/random");
std::cout << "*handle : " << *handle << std::endl;
std::cout << "*handle.get(): " << *handle.get() << std::endl;
}


My output (live output here):



*handle : 4198400
*handle.get(): 3


Please note that *handle.get() returns the correct value, while *handle doesn't.


Why am I getting different results?


Aucun commentaire:

Enregistrer un commentaire