自动生成移动操作和原始指针成员 [英] Automatic generated move operations and raw pointer members

查看:102
本文介绍了自动生成移动操作和原始指针成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个 MemoryMappedFile 类,其中包含以下数据成员:

Consider a MemoryMappedFile class, with the following data members:

class MemoryMappedFile
{
....
private:
    // RAII wrapper to some OS-specific raw handle (e.g. Win32 HANDLE),
    // representing the memory-mapped file.
    Handle m_handle;

    // Pointer to file begin (first file byte).
    BYTE* m_first;

    // Pointer to file end (one byte past last file byte).
    BYTE* m_last;
};

Handle 类是一些RAII包装器一些特定的OS原始C样句柄(例如认为Win32 HANDLE )。不可复制,但它是可移动的。

相反, m_first m_last 是与文件内容映射的内存区域中的原始指针。

The Handle class is some RAII wrapper to some specific OS raw C-like handle (e.g. think of Win32 HANDLE). It is not copyable, but it is movable.
Instead, m_first and m_last are raw pointers inside the memory area mapped with the file content.

我想要 MemoryMappedFile 类为可移动(但不能复制,就像 Handle 类)。

I'd like the MemoryMappedFile class to be movable (but not copyable, just like the Handle class).

如果不是针对原始指针,根据C ++ 11的自动通过成员方式移动生成移动构造函数的规则,类将是

If it weren't for the raw pointers, according to C++11's rules of automatic generation of move constructor by member-wise moves, the class would be automatically movable.

不幸的是,原始指针迫使我编写一个自定义移动构造函数:

Unfortunately, the raw pointers force me to write a custom move constructor:

MemoryMappedFile::MemoryMappedFile(MemoryMappedFile&& other)
    : m_handle( std::move(other.m_handle) )
{
    // Move m_first
    m_first = other.m_first;
    other.m_first = nullptr;

    // Move m_last
    m_last = other.m_last;
    other.m_last = nullptr;
}

如果C ++标准库有某种形式的 (移动构造函数和移动分配)的原始指针(其作为非拥有指针)是非常简单的,可对准智能但可移动的指针,这样编译器可以在具有这些指针作为数据成员的类中自动生成正确的移动操作。

It would be nice if C++ standard library had some form of "dumb-as-opposed-to-smart but movable" pointer, with zero-overhead, just like raw pointers (which are fine as observing non-owning pointers), but with move operations (move constructor and move assignment) defined, such that the compiler can automatically generate correct move operations in classes that have these pointers as data members.

在C ++标准库中有这样的东西, ?

Is there something like this in C++ standard library, or in Boost?

还是有其他方法来实现相同的目标(除了编写我自己的自定义ObservingPointer类,包装原始指针和定义移动操作)?

Or is there any other way to achieve the same goal (beside writing my own custom ObservingPointer class, wrapping raw pointers and defining move operations)?

推荐答案

我经常需要一个智能指针,既可复制和移动,但有一个明确定义的移出状态, a href =https://gitlab.com/redistd/redistd/blob/master/include/redi/tidy_ptr.h =nofollow> tidy_ptr 这是一个哑智能指针,除了零本身在移动之外没有什么特别的。这种类型是可复制的,所以要获得你想要的类的语义,你仍然需要将复制操作定义为已删除(或者使用 std :: unique_ptr -op deleter)。

I frequently need a "smart" pointer that is both copyable and movable, but has a well-defined moved-from state, so I wrote tidy_ptr which is a "dumb" smart pointer that does nothing special except zero itself on move. That type is copyable, so to get the semantics you want for your class you would still need to define the copy operations as deleted (or just use std::unique_ptr with a no-op deleter).

我试图说服标准委员会, observer_ptr ,世界上最愚蠢的智能指针,应该有这种行为,但一致的是,它应该像一个内置指针(除了在构造函数中的零初始化)。我仍然认为它应该零移动。该文件显示了一个 non_owning_ptr ,它是一个带有无操作删除器的 unique_ptr 的别名,它会执行你想要的操作。

I've tried to convince the standards committee that observer_ptr, "the world's dumbest smart pointer", should have this behaviour, but the consensus was that it should behave just like a built-in pointer (except for zero-initialization in the constructor). I still think it should zero on move. That paper shows a non_owning_ptr which is an alias for unique_ptr with a no-op deleter, which does what you want.

这篇关于自动生成移动操作和原始指针成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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