简体   繁体   中英

Scoped std::unique_ptr cast

I'm currently working on some code using smart pointers in which it is necessary at a number of points to cast these pointers to their base types and pass them as const arguments to functions. Currently I'm using shared_ptr's and the standard pointer casting functions to achieve this, but this seems inefficient (as each cast costs at least one CAS) and also misleading (as we are not modelling a shared relationship, the parent is the sole owner of the object).

I therefore came up with the following but wanted to check it is indeed safe, or is there some edge case which will break it?

template <typename ToType, typename FromType>
class FTScopedCastWrapper {
public:
    explicit FTScopedCastWrapper(std::unique_ptr<FromType>& p) : from_ptr_(&p) {
        auto d = static_cast<ToType *>(p.release());
        to_ptr_ = std::unique_ptr<ToType>(d);
    }

    ~FTScopedCastWrapper() {
        auto d = static_cast<FromType *>(to_ptr_.release());
        (*from_ptr_) = std::unique_ptr<FromType>(d);
    }

    const std::unique_ptr<ToType>& operator()() {
        return to_ptr_;
    }


    // Prevent allocation on the heap
    void* operator new(size_t) = delete;
    void* operator new(size_t, void*) = delete;
    void* operator new[](size_t) = delete;
    void* operator new[](size_t, void*) = delete;

private:
    std::unique_ptr<FromType>* from_ptr_;
    std::unique_ptr<ToType> to_ptr_;
};

template <typename ToType, typename FromType>
FTScopedCastWrapper<ToType, FromType> FTScopedCast(std::unique_ptr<FromType>& p) {
    return FTScopedCastWrapper<ToType, FromType>(p);
}

The intended usage is then

void testMethod(const std::unique_ptr<Base>& ptr) {
    // Do Stuff
}

auto ptr = std::make_unique<Derived>();
testMethod(FTScopedCast<Base>(ptr)());

The deleter is not carried across as doing so would prevent upcasting. It also doesn't make sense to do so as the deleter will never be invoked on the created smart pointer anyway.

Allocation on the heap is prevented as it could allow the wrapper to outlive the pointer it wraps, copying is prevented by the std::unique_ptr member and standard destruction order will ensure the raw pointer is returned to the original smart pointer before it is destroyed, even if it is declared in the same scope as the wrapper.

I'm aware this is not thread safe but I'd argue sharing a unique_ptr between threads is breaking its contract of a single owner.

If I understand you correctly, the intention is to "steal" the contents of a std::unique_ptr for the duration of the function call, and then return it to its original owner when the function call is complete.

But this just seems needlessly convoluted. For a start, as pointed out by @TheUndeadFish in the comments, you could just take a raw Base* as the function argument and call it with std::unique_ptr::get() . As long as the called function doesn't do something silly like call delete on the passed-in pointer or squirrel it away in a static variable for later use then this will work just fine.

Alternatively, if you find raw pointers completely distasteful, you could use a non-owning pointer wrapper, something like the following (untested, but you get the idea):

template <typename T>
class unowned_ptr {
public:
    unowned_ptr() = default;
    unowned_ptr(const unowned_ptr&) = default;
    unowned_ptr& operator=(const unowned_ptr&) = default;

    template <typename U>
    unowned_ptr(const U* other) : ptr(other) {}

    template <typename U>
    unowned_ptr(const std::unique_ptr<U>& other) : ptr(other.get()) {}

    T* operator->() { return ptr; }
    const T* operator->() const { return ptr; }

private:
    T* ptr = nullptr;
};

Something very similar to this, std::observer_ptr ("the world's dumbest smart pointer") was proposed for C++17, but I'm not sure of the status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM