Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Edit: I'll leave this answer for completeness but you probably shouldn't inherit from a smart pointer or be very careful when doing so: is-it-ok-to-inherit-from-the-c11-smart-pointers-and-override-the-relative-operis-it-ok-to-inherit-from-the-c11-smart-pointers-and-override-the-relative-oper

It seems to me like there's lot of code that simply wraps the functionality of the unique_ptr. Putting aside that having a copyable unique_ptr seems a bit dubious (although it seems like a reasonable thing in this context) why don't we inherit from unique_ptr and provide the copy operators?

template <typename T>
struct pimpl_ptr : public std::unique_ptr<T>
{
    using std::unique_ptr<T>::unique_ptr;
    pimpl_ptr() {};
    pimpl_ptr(pimpl_ptr<T> const& other)
    { 
        auto value = *other.get();
        this->reset(new T(value));
    }
    pimpl_ptr<T>& operator=(pimpl_ptr<T> const& other)
    {
        auto value = *other.get();
        this->reset(new T(value));
        return *this;
    }
};

There's probably a handful of improvements that could be made, as per Loki Astari's answer, but I think conceptually this will do the same but in less lines of code.

Edit: I'll leave this answer for completeness but you probably shouldn't inherit from a smart pointer or be very careful when doing so: is-it-ok-to-inherit-from-the-c11-smart-pointers-and-override-the-relative-oper

It seems to me like there's lot of code that simply wraps the functionality of the unique_ptr. Putting aside that having a copyable unique_ptr seems a bit dubious (although it seems like a reasonable thing in this context) why don't we inherit from unique_ptr and provide the copy operators?

template <typename T>
struct pimpl_ptr : public std::unique_ptr<T>
{
    using std::unique_ptr<T>::unique_ptr;
    pimpl_ptr() {};
    pimpl_ptr(pimpl_ptr<T> const& other)
    { 
        auto value = *other.get();
        this->reset(new T(value));
    }
    pimpl_ptr<T>& operator=(pimpl_ptr<T> const& other)
    {
        auto value = *other.get();
        this->reset(new T(value));
        return *this;
    }
};

There's probably a handful of improvements that could be made, as per Loki Astari's answer, but I think conceptually this will do the same but in less lines of code.

Edit: I'll leave this answer for completeness but you probably shouldn't inherit from a smart pointer or be very careful when doing so: is-it-ok-to-inherit-from-the-c11-smart-pointers-and-override-the-relative-oper

It seems to me like there's lot of code that simply wraps the functionality of the unique_ptr. Putting aside that having a copyable unique_ptr seems a bit dubious (although it seems like a reasonable thing in this context) why don't we inherit from unique_ptr and provide the copy operators?

template <typename T>
struct pimpl_ptr : public std::unique_ptr<T>
{
    using std::unique_ptr<T>::unique_ptr;
    pimpl_ptr() {};
    pimpl_ptr(pimpl_ptr<T> const& other)
    { 
        auto value = *other.get();
        this->reset(new T(value));
    }
    pimpl_ptr<T>& operator=(pimpl_ptr<T> const& other)
    {
        auto value = *other.get();
        this->reset(new T(value));
        return *this;
    }
};

There's probably a handful of improvements that could be made, as per Loki Astari's answer, but I think conceptually this will do the same but in less lines of code.

Realised that inheriting from a smart pointer isn't such a great idea so included a warning in my answer.
Source Link
user975326
  • 223
  • 1
  • 8

Edit: I'll leave this answer for completeness but you probably shouldn't inherit from a smart pointer or be very careful when doing so: is-it-ok-to-inherit-from-the-c11-smart-pointers-and-override-the-relative-oper

It seems to me like there's lot of code that simply wraps the functionality of the unique_ptr. Putting aside that having a copyable unique_ptr seems a bit dubious (although it seems like a reasonable thing in this context) why don't we inherit from unique_ptr and provide the copy operators?

template <typename T>
struct pimpl_ptr : public std::unique_ptr<T>
{
    using std::unique_ptr<T>::unique_ptr;
    pimpl_ptr() {};
    pimpl_ptr(pimpl_ptr<T> const& other)
    { 
        auto value = *other.get();
        this->reset(new T(value));
    }
    pimpl_ptr<T>& operator=(pimpl_ptr<T> const& other)
    {
        auto value = *other.get();
        this->reset(new T(value));
        return *this;
    }
};

There's probably a handful of improvements that could be made, as per Loki Astari's answer, but I think conceptually this will do the same but in less lines of code.

It seems to me like there's lot of code that simply wraps the functionality of the unique_ptr. Putting aside that having a copyable unique_ptr seems a bit dubious (although it seems like a reasonable thing in this context) why don't we inherit from unique_ptr and provide the copy operators?

template <typename T>
struct pimpl_ptr : public std::unique_ptr<T>
{
    using std::unique_ptr<T>::unique_ptr;
    pimpl_ptr() {};
    pimpl_ptr(pimpl_ptr<T> const& other)
    { 
        auto value = *other.get();
        this->reset(new T(value));
    }
    pimpl_ptr<T>& operator=(pimpl_ptr<T> const& other)
    {
        auto value = *other.get();
        this->reset(new T(value));
        return *this;
    }
};

There's probably a handful of improvements that could be made, as per Loki Astari's answer, but I think conceptually this will do the same but in less lines of code.

Edit: I'll leave this answer for completeness but you probably shouldn't inherit from a smart pointer or be very careful when doing so: is-it-ok-to-inherit-from-the-c11-smart-pointers-and-override-the-relative-oper

It seems to me like there's lot of code that simply wraps the functionality of the unique_ptr. Putting aside that having a copyable unique_ptr seems a bit dubious (although it seems like a reasonable thing in this context) why don't we inherit from unique_ptr and provide the copy operators?

template <typename T>
struct pimpl_ptr : public std::unique_ptr<T>
{
    using std::unique_ptr<T>::unique_ptr;
    pimpl_ptr() {};
    pimpl_ptr(pimpl_ptr<T> const& other)
    { 
        auto value = *other.get();
        this->reset(new T(value));
    }
    pimpl_ptr<T>& operator=(pimpl_ptr<T> const& other)
    {
        auto value = *other.get();
        this->reset(new T(value));
        return *this;
    }
};

There's probably a handful of improvements that could be made, as per Loki Astari's answer, but I think conceptually this will do the same but in less lines of code.

fixed typos
Source Link
user975326
  • 223
  • 1
  • 8

It seems to me like there's lot of code that simply wraps the functionality of the unique_ptr. Putting aside that having a copyable unique_ptr seems a bit dubious (although it seems like a reasonable thing in this context) why don't we inherit from unique_ptr and provide the copy operators?

template <typename T>
struct pimpl_ptr : public std::unique_ptr<T>
{
    using std::unique_ptr<T>::unique_ptr;
    pimpl_ptr() {};
    pimpl_ptr(pimp_ptr<T>pimpl_ptr<T> const& other)
    { 
        auto value = *other.get();
        this->reset(new T(value));
    }
    pimp_ptr<T>&pimpl_ptr<T>& operator=(pimp_ptr<T>pimpl_ptr<T> const& other)
    {
        auto value = *other.get();
        this->reset(new T(value));
        return *this;
    }
};

There's probably a handful of improvements that could be made, as per Loki Astari's answer, but I think conceptually this will do the same but in less lines of code.

It seems to me like there's lot of code that simply wraps the functionality of the unique_ptr. Putting aside that having a copyable unique_ptr seems a bit dubious (although it seems like a reasonable thing in this context) why don't we inherit from unique_ptr and provide the copy operators?

template <typename T>
struct pimpl_ptr : public std::unique_ptr<T>
{
    using std::unique_ptr<T>::unique_ptr;
    pimpl_ptr() {};
    pimpl_ptr(pimp_ptr<T> const& other)
    { 
        auto value = *other.get();
        this->reset(new T(value));
    }
    pimp_ptr<T>& operator=(pimp_ptr<T> const& other)
    {
        auto value = *other.get();
        this->reset(new T(value));
        return *this;
    }
};

There's probably a handful of improvements that could be made, as per Loki Astari's answer, but I think conceptually this will do the same but in less lines of code.

It seems to me like there's lot of code that simply wraps the functionality of the unique_ptr. Putting aside that having a copyable unique_ptr seems a bit dubious (although it seems like a reasonable thing in this context) why don't we inherit from unique_ptr and provide the copy operators?

template <typename T>
struct pimpl_ptr : public std::unique_ptr<T>
{
    using std::unique_ptr<T>::unique_ptr;
    pimpl_ptr() {};
    pimpl_ptr(pimpl_ptr<T> const& other)
    { 
        auto value = *other.get();
        this->reset(new T(value));
    }
    pimpl_ptr<T>& operator=(pimpl_ptr<T> const& other)
    {
        auto value = *other.get();
        this->reset(new T(value));
        return *this;
    }
};

There's probably a handful of improvements that could be made, as per Loki Astari's answer, but I think conceptually this will do the same but in less lines of code.

Source Link
user975326
  • 223
  • 1
  • 8
Loading