移动语义==自定义交换函数过时了吗?
最近,很多问题 弹出窗口介绍如何提供您自己的交换函数。在 C++11 中,
std::swap
将使用 std::move
和移动语义来尽快交换给定值。当然,只有当您提供移动构造函数和移动赋值运算符(或使用按值传递的运算符)时,这才有效。
现在,既然如此,实际上是否有必要在 C++11 中编写自己的 swap
函数?我只能想到不可移动类型,但话又说回来,自定义交换通常通过某种“指针交换”(也称为移动)来工作。也许有某些参考变量?嗯...
Recently, many questions pop up on how to provide your own swap
function. With C++11, std::swap
will use std::move
and move semantics to swap the given values as fast as possible. This, of course, only works if you provide a move constructor and a move assignment operator (or one that uses pass-by-value).
Now, with that given, is it actually necessary to write your own swap
functions in C++11? I could only think of non-movable types, but then again, the custom swap
s usually work through some kind of "pointer exchange" (aka moving). Maybe with certain reference variables? Hm...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个判断问题。我通常会让
std::swap
完成原型代码的工作,但对于发布代码,编写自定义交换。我通常可以编写一个自定义交换,其速度大约是 1 个移动构造 + 2 个移动分配 + 1 个无资源破坏的速度的两倍。然而,人们可能需要等到 std::swap 实际上被证明是一个性能问题后再去麻烦。Alf P. Steinbach 更新:
20.2.2 [utility.swap] 指定
std::swap(T&, T&)
有一个noexcept 相当于:
即,如果
T
上的移动操作为noexcept
,则T
上的std::swap
为无例外
。请注意,此规范不需要移动成员。它只要求存在右值的构造和赋值,如果是
noexcept
,则 swap 将为noexcept
。例如:std::swap
是 noexcept,即使没有 move 成员。It is a matter of judgment. I will typically let
std::swap
do the job for prototyping code, but for release code write a custom swap. I can usually write a custom swap that is about twice as fast as 1 move construction + 2 move assignments + 1 resourceless destruction. However one may want to wait untilstd::swap
actually proves to be a performance problem before going to the bother.Update for Alf P. Steinbach:
20.2.2 [utility.swap] specifies that
std::swap(T&, T&)
has anoexcept
equivalent to:I.e. if move operations on
T
arenoexcept
, thenstd::swap
onT
isnoexcept
.Note that this spec doesn't require move members. It only requires that construction and assignment from rvalues exists, and if it is
noexcept
, then swap will benoexcept
. E.g.:std::swap<A>
is noexcept, even without move members.当然,您可以将交换实现为
但是我们可能有自己的类,例如
A
,我们可以更快地交换它。其中,不必运行构造函数和析构函数,只需交换指针(很可能以 XCHG 或类似的方式实现)。
当然,编译器可能会优化第一个示例中的构造函数/析构函数调用,但如果它们有副作用(即调用 new/delete),它可能不够聪明,无法优化它们。
Sure, you can implement swap as
But we might have our own class, say
A
, which we can swap more quickly.Which, instead of having to run a constructor and destructor, just swaps the pointers (which may well be implemented as XCHG or something similar).
Of course, the compiler might optimize out the constructor/destructor calls in the first example, but if they have side effects (i.e. calls to new/delete) it may not be smart enough to optimize them away.
可能有些类型可以交换但不能移动。我不知道有什么不可移动的类型,所以我没有任何例子。
There might be some types that can be swapped but not moved. I don't know of any non-movable types, so I don't have any examples.
按照惯例,自定义
交换
提供无抛出保证。我不知道std::swap
。我对委员会在这方面的工作的印象是,这都是政治性的,所以如果他们在某个地方将duck
定义为bug
或类似的政治文字游戏,我不会感到惊讶演习。因此,我不会依赖这里的任何答案,除非它提供了详细的逐一引用 C++0x 的标准,直到最小的细节(以确保没有错误)。
By convention a custom
swap
offers no-throw guarantee. I don't know aboutstd::swap
. My impression of the committee's work on that is that it was all political, so it would not surprise me if they somewhere had definedduck
asbug
, or similar political word-game maneuvers. So I would not rely on any answer here unless it provides a detailed blow by blow quoting from the C++0x to-be-standard, down the smallest detail (so as to be sure nobug
).考虑以下持有内存分配资源的类(为简单起见,用单个整数表示):
然后
std::swap
导致删除空指针 3 次(对于移动赋值运算符和统一赋值运算符情况)。编译器可能无法优化此类删除
,请参阅https://godbolt.org /g/E84ud4。自定义
交换
不会调用任何删除
,因此可能更高效。我想这就是std::unique_ptr
提供自定义std::swap
专业化的原因。更新
看来 Intel 和 Clang 编译器能够优化空指针的删除,但 GCC 却不能。请参阅 为什么 GCC 不优化删除C++ 中的空指针? 了解详细信息。
UPDATE
看来,使用GCC,我们可以通过重写
X
来防止调用delete
运算符,如下所示:Consider the following class that holds a memory-allocated resource (for simplicity, represented by a single integer number):
Then
std::swap
results in deleting null pointer 3 times (both for move assignment operator and unifying assignment operator cases). Compilers might have problem to optimize out such adelete
, see https://godbolt.org/g/E84ud4.Custom
swap
does not call anydelete
and might be therefore more efficient. I guess this is the reason whystd::unique_ptr
provides customstd::swap
specialization.UPDATE
It seems that Intel and Clang compilers are able to optimize out deletion of null pointers, however GCC isn't. See Why GCC doesn't optimize out deletion of null pointers in C++? for details.
UPDATE
It seems that with GCC, we can prevent invoking
delete
operator by rewritingX
as follows: