gpt4 book ai didi

c++ - 将赋值运算符实现为 "destroy + construct"是否合法?

转载 作者:行者123 更新时间:2023-12-02 02:42:41 24 4
gpt4 key购买 nike

我经常需要为“原始”资源句柄实现 C++ 包装器,例如文件句柄、Win32 操作系统句柄等。执行此操作时,我还需要实现 move 运算符,因为编译器生成的默认运算符不会清除移出的对象,从而产生双删除问题。

在实现 move 赋值运算符时,我更喜欢显式调用析构函数并使用新的放置就地重新创建对象。这样,我就避免了析构函数逻辑的重复。此外,我经常以复制+ move 的方式实现复制分配(如果相关的话)。这导致以下代码:

/** Canonical move-assignment operator. 
Assumes no const or reference members. */
TYPE& operator = (TYPE && other) noexcept {
if (&other == this)
return *this; // self-assign

static_assert(std::is_final<TYPE>::value, "class must be final");
static_assert(noexcept(this->~TYPE()), "dtor must be noexcept");
this->~TYPE();

static_assert(noexcept(TYPE(std::move(other))), "move-ctor must be noexcept");
new(this) TYPE(std::move(other));
return *this;
}

/** Canonical copy-assignment operator. */
TYPE& operator = (const TYPE& other) {
if (&other == this)
return *this; // self-assign

TYPE copy(other); // may throw

static_assert(noexcept(operator = (std::move(copy))), "move-assignment must be noexcept");
operator = (std::move(copy));
return *this;
}

这让我觉得很奇怪,但我没有在网上看到任何以这种“规范”方式实现 move +复制赋值运算符的建议。相反,大多数网站倾向于以特定于类型的方式实现赋值运算符,在维护类时必须手动与构造函数和析构函数保持同步。

是否有任何争论(除了性能之外)反对以这种与类型无关的“规范”方式实现 move 和复制赋值运算符?

根据 UB 评论更新 2019-10-08:

我已通读 http://eel.is/c++draft/basic.life#8这似乎涵盖了有关案件。摘录:

If, after the lifetime of an object has ended ..., a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, ... will automatically refer to the new object and, ..., can be used to manipulate the new object, if ...

此后有一些与相同类型和常量/引用成员相关的明显条件,但它们似乎是任何赋值运算符实现所必需的。如果我错了,请纠正我,但在我看来,我的“规范”样本表现良好,不是 UB(?)

根据 copy-and-swap 评论更新 2019-10-10:

赋值实现可以合并到一个采用值参数而不是引用的方法中。这似乎也消除了对 static_assert 和自分配检查的需要。我新提议的实现将变为:

/** Canonical copy/move-assignment operator.
Assumes no const or reference members. */
TYPE& operator = (TYPE other) noexcept {
static_assert(!std::has_virtual_destructor<TYPE>::value, "dtor cannot be virtual");
this->~TYPE();
new(this) TYPE(std::move(other));
return *this;
}

最佳答案

有人强烈反对你的“规范”实现 - 这是错误的。

您结束原始对象的生命周期并在其位置创建一个新对象。 但是,原始对象的指针、引用等不会自动更新为指向新对象——你必须使用std::launder(这句话是对于大多数类来说都是错误的;请参阅 Davis Herring 的评论。)然后,在原始对象上自动调用析构函数,触发 undefined behavior .

引用:(强调我的)[class.dtor]/16

Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended.Example: If the destructor for an automatic object is explicitly invoked, and the block is subsequently left in a manner that would ordinarily invoke implicit destruction of the object, the behavior is undefined. — end example ]

[basic.life]/1

[...] The lifetime of an object o of type T ends when:

  • if T is a class type with a non-trivial destructor ([class.dtor]), the destructor call starts, or

  • the storage which the object occupies is released, or is reused by an object that is not nested within o ([intro.object]).

(根据类的析构函数是否平凡,结束对象生命周期的代码行是不同的。如果析构函数是非平凡的,则显式调用析构函数会结束对象的生命周期;否则,放置 new 重用当前对象的存储,结束其生命周期。无论哪种情况,当赋值运算符返回时,对象的生命周期都已结束。)

<小时/>

您可能认为这是另一种“任何理智的实现都会做正确的事情”类型的未定义行为,但实际上许多编译器优化都涉及缓存值,这利用了此规范。因此,当代码在不同的优化级别下、由不同的编译器、使用同一编译器的不同版本编译时,或者当编译器刚刚经历了糟糕的一天并且心情不好时,您的代码可能随时中断。

<小时/>

实际的“规范”方法是使用 copy-and-swap idiom :

// copy constructor is implemented normally
C::C(const C& other)
: // ...
{
// ...
}

// move constructor = default construct + swap
C::C(C&& other) noexcept
: C{}
{
swap(*this, other);
}

// assignment operator = (copy +) swap
C& C::operator=(C other) noexcept // C is taken by value to handle both copy and move
{
swap(*this, other);
return *this;
}

请注意,在这里,您需要提供自定义的 swap 函数,而不是使用 std::swap,如 Howard Hinnant 提到的:

friend void swap(C& lhs, C& rhs) noexcept
{
// swap the members
}

如果使用得当,如果相关函数被正确内联(这应该是相当微不足道的), copy-and-swap 不会产生任何开销。这个习惯用法非常常用,一般的 C++ 程序员应该很容易理解它。不用担心它会造成困惑,只需花 2 分钟来学习它,然后使用它。

这一次,我们交换了对象的值,并且对象的生命周期不受影响。该对象仍然是原始对象,只是具有不同的值,而不是全新的对象。可以这样想:你想阻止一个 child 欺负别人。交换值(value)观就像对他们进行文明教育,而“破坏+ build ”就像杀死他们让他们暂时死亡并给他们一个全新的大脑(可能需要魔法的帮助)。至少可以说,后一种方法可能会产生一些不良副作用。

像任何其他习语一样,在适当的时候使用它 - 不要只是为了使用它而使用它。

关于c++ - 将赋值运算符实现为 "destroy + construct"是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58280104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com