According to others, the std::function move constructor has NOT noexcept according to the C ++ standard.
As Peter Ruderman said, a developer can define a noexcept move noexcept using the swap method (which conforms to the noexcept standard).
And the GCC implementation actually uses this technique and defines the std::function move constructor as noexcept :
function(function&& __x) noexcept : _Function_base() { __x.swap(*this); }
So, if you use GCC, you can assume that the std::function move constructor has noexcept value. But another implementation of the C ++ standard library may have a different implementation of the move constructor. Therefore, you should not rely on this.
It is better to have the default construct std::function ( noexcept ), and then use the swap method:
This is not as graceful as using the move constructor, but at least it is portable.
And be careful with the movement of semantics !!! This can do something that you do not expect, because it is usually indicated that the object is moving from a valid, but unspecified . For example:
#include <iostream>
The GCC implementation does not print "Hello world", but another implementation may be implemented. The correct way is to explicitly clear the object moved from:
hello = std::move(a.fn); a.fn = nullptr;
But this makes moving semantically inconvenient to use (IMO).
anton_rh
source share