gpt4 book ai didi

c++ - 分配给不同的基类时,我可以多次使用 std::move 吗?

转载 作者:行者123 更新时间:2023-12-05 03:29:47 25 4
gpt4 key购买 nike

HashTable & operator= (HashTable && rhs)
{
destroyElements();
free();

std::swap(buf, rhs.buf);
std::swap(m_size, rhs.m_size);
std::swap(grower, rhs.grower);

Hash::operator=(std::move(rhs));
Allocator::operator=(std::move(rhs));
Cell::State::operator=(std::move(rhs));
ZeroValueStorage<Cell::need_zero_value_storage, Cell>::operator=(std::move(rhs));

return *this;
}

调用Hash::operator=rhs会失效吗?

最佳答案

假设这些基地都没有做一些下流的事情,比如扰乱其他基地的状态,那么它应该没问题。

Hash::operator=(std::move(rhs));

转换为右值会在此处生成一个 HashTable&&。这反过来又可以隐式绑定(bind)到 Hash&&(假设有一个可访问且明确的基础)。所以运算符只对Hash子对象进行操作。并且您不会再次访问那个对象,遵循在对象被移出后不使用该对象的指导原则。

其他基地也是如此。

总而言之,如果您的类行为良好并且在移动分配期间正确维护它们自己的不变量。这是实现该举措的可行方式。哎呀,即使是默认的移动操作委托(delegate)给基地的移动操作。

关于c++ - 分配给不同的基类时,我可以多次使用 std::move 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70955414/

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