- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我经常需要为“原始”资源句柄实现 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 和复制赋值运算符?
我已通读 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(?)
赋值实现可以合并到一个采用值参数而不是引用的方法中。这似乎也消除了对 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;
}
最佳答案
有人强烈反对你的“规范”实现 - 这是错误的。
您结束原始对象的生命周期并在其位置创建一个新对象。 但是,原始对象的指针、引用等不会自动更新为指向新对象——你必须使用(这句话是对于大多数类来说都是错误的;请参阅 Davis Herring 的评论。)然后,在原始对象上自动调用析构函数,触发 undefined behavior .std::launder
。
引用:(强调我的)[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 ]
[...] 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, orthe 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/
我有一个 if 语句,如下所示 if (not(fullpath.lower().endswith(".pdf")) or not (fullpath.lower().endswith(tup
然而,在 PHP 中,可以: only appears if $foo is true. only appears if $foo is false. 在 Javascript 中,能否在一个脚
XML有很多好处。它既是机器可读的,也是人类可读的,它具有标准化的格式,并且用途广泛。 它也有一些缺点。它是冗长的,不是传输大量数据的非常有效的方法。 XML最有用的方面之一是模式语言。使用模式,您可
由于长期使用 SQL2000,我并没有真正深入了解公用表表达式。 我给出的答案here (#4025380)和 here (#4018793)违背了潮流,因为他们没有使用 CTE。 我很欣赏它们对于递
我有一个应用程序: void deleteObj(id){ MyObj obj = getObjById(id); if (obj == null) { throw n
我的代码如下。可能我以类似的方式多次使用它,即简单地说,我正在以这种方式管理 session 和事务: List users= null; try{ sess
在开发J2EE Web应用程序时,我通常会按以下方式组织我的包结构 com.jameselsey.. 控制器-控制器/操作转到此处 服务-事务服务类,由控制器调用 域-应用程序使用的我的域类/对象 D
这更多是出于好奇而不是任何重要问题,但我只是想知道 memmove 中的以下片段文档: Copying takes place as if an intermediate buffer were us
路径压缩涉及将根指定为路径上每个节点的新父节点——这可能会降低根的等级,并可能降低路径上所有节点的等级。有办法解决这个问题吗?有必要处理这个吗?或者,也许可以将等级视为树高的上限而不是确切的高度? 谢
我有两个类,A 和 B。A 是 B 的父类,我有一个函数接收指向 A 类型类的指针,检查它是否也是 B 类型,如果是将调用另一个函数,该函数接受一个指向类型 B 的类的指针。当函数调用另一个函数时,我
有没有办法让 valgrind 使用多个处理器? 我正在使用 valgrind 的 callgrind 进行一些瓶颈分析,并注意到我的应用程序中的资源使用行为与在 valgrind/callgrind
假设我们要使用 ReaderT [(a,b)]超过 Maybe monad,然后我们想在列表中进行查找。 现在,一个简单且不常见的方法是: 第一种可能性 find a = ReaderT (looku
我的代码似乎有问题。我需要说的是: if ( $('html').attr('lang').val() == 'fr-FR' ) { // do this } else { // do
根据this文章(2018 年 4 月)AKS 在可用性集中运行时能够跨故障域智能放置 Pod,但尚不考虑更新域。很快就会使用更新域将 Pod 放入 AKS 中吗? 最佳答案 当您设置集群时,它已经自
course | section | type comart2 : bsit201 : lec comart2 :
我正在开发自己的 SDK,而这又依赖于某些第 3 方 SDK。例如 - OkHttp。 我应该将 OkHttp 添加到我的 build.gradle 中,还是让我的 SDK 用户包含它?在这种情况下,
随着 Rust 越来越充实,我对它的兴趣开始激起。我喜欢它支持代数数据类型,尤其是那些匹配的事实,但是对其他功能习语有什么想法吗? 例如标准库中是否有标准过滤器/映射/归约函数的集合,更重要的是,您能
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我一直在研究 PHP 中的对象。我见过的所有示例甚至在它们自己的对象上都使用了对象构造函数。 PHP 会强制您这样做吗?如果是,为什么? 例如: firstname = $firstname;
...比关联数组? 关联数组会占用更多内存吗? $arr = array(1, 1, 1); $arr[10] = 1; $arr[] = 1; // <- index is 11; does the
我是一名优秀的程序员,十分优秀!