防止 std::move on object? [英] Prevent std::move on object?

查看:26
本文介绍了防止 std::move on object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非空的 unique_ptr.

I'm trying to create a not-null unique_ptr.

template <typename T>
class unique_ref {
public:
    template <class... Types>
    unique_ref(Types&&... Args) { mPtr = std::make_unique<T, Types...>(std::forward<Types>(Args)...); }
    T* release() && { return mPtr.release(); }
    T* release() & = delete;

private:
    std::unique_ptr<T> mPtr;
};

我的目标是仅当 unique_ref 是临时的时才允许 release().

My goal is to allow release() only if the unique_ref is a temporary.

问题是有人可以使用 std::move() 来绕过"这个:

The problem is someone could use std::move() to "get around" this:

unique_ref<int> p;
int* p2 = std::move(p).release();

有没有办法防止它被move'd?

Is there a way to prevent it from being move'd?

推荐答案

您将不得不放弃 std::move 案例.当用户调用 std::move 时,他们发出了一个强烈的信号,表明他们确切地知道自己在做什么.

You're going to have to let the std::move case go. When a user invokes std::move, they are giving a strong signal that they know exactly what they are doing.

您可以在调试期间保护自己.

You can protect yourself though during debug time.

例如,我会考虑像这样开始类定义:

For example, I would consider starting the class definition a little like this:

#include <memory>
#include <cassert>

template <typename T>
class unique_ref {
public:
    // a number of problems here, but that is a discussuion for another day
    template <class... Types>
    unique_ref(Types&&... Args) 
    : mPtr(std::make_unique<T>(std::forward<Types>(Args)...))
    { }

    // unique_ref is implicitly move-only

    // see check below
    bool has_value() const {
        return bool(mPtr);
    }

    // here I am implicitly propagating the container's constness to the 
    // inner reference yielded. You may not want to do that.
    // note that all these accessors are marshalled through one static function
    // template. This gives me control of behaviour in exactly one place. 
    // (DRY principles)
    auto operator*() -> decltype(auto) {
        return *get_ptr(this);
    }

    auto operator*() const -> decltype(auto) {
        return *get_ptr(this);
    }

    auto operator->() -> decltype(auto) {
        return get_ptr(this);
    }

    auto operator->() const -> decltype(auto) {
        return get_ptr(this);
    }

private:
    using implementation_type = std::unique_ptr<T>;
    implementation_type release() { return std::move(mPtr); }

    // this function is deducing constness of the container and propagating it
    // that may not be what you want.
    template<class MaybeConst>
    static auto get_ptr(MaybeConst* self) -> decltype(auto)
    {
        auto ptr = self->mPtr.get();
        assert(ptr);
        using self_type = std::remove_pointer_t<decltype(self)>;
        if constexpr (std::is_const<self_type>())
            return static_cast<T const*>(ptr);
        else
            return ptr;
    }

private:
    implementation_type mPtr;
};

struct foo
{
};

auto generate()->unique_ref<foo> {
    return unique_ref<foo>();
}

void test()
{
    auto rfoo1 = generate();
    auto rfoo2 = generate();
//    auto rfoo3 = rfoo1; not copyable

    // we have to assume that a user knows what he's doing here
    auto rfoo3 = std::move(rfoo1);

    // but we can add a check
    assert(!rfoo1.has_value());

    auto& a = *rfoo3;
    static_assert(!std::is_const<std::remove_reference_t<decltype(a)>>());

    const auto rfoo4 = std::move(rfoo3);
    auto& b = *rfoo4;
    static_assert(std::is_const<std::remove_reference_t<decltype(b)>>());
}

这篇关于防止 std::move on object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆