Consider:
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
class Gizmo
{
public:
Gizmo() : foo_(shared_ptr<string>(new string("bar"))) {};
Gizmo(Gizmo&& rhs); // Implemented Below
private:
shared_ptr<string> foo_;
};
/*
// doesn't use std::move
Gizmo::Gizmo(Gizmo&& rhs)
: foo_(rhs.foo_)
{
}
*/
// Does use std::move
Gizmo::Gizmo(Gizmo&& rhs)
: foo_(std::move(rhs.foo_))
{
}
int main()
{
typedef vector<Gizmo> Gizmos;
Gizmos gizmos;
generate_n(back_inserter(gizmos), 10000, []() -> Gizmo
{
Gizmo ret;
return ret;
});
random_shuffle(gizmos.begin(), gizmos.end());
}
In the above code, there are two versions of Gizmo::Gizmo(Gizmo&&)
-- one uses std::move
to actually move the shared_ptr
, and the other just copies the shared_ptr
.
Both version seem to work on the surface. One difference (the only difference I can see) is in the non-move
version the reference count of the shared_ptr
is temporarily increased, but only briefly.
I would normally go ahead and move
the shared_ptr
, but only to be clear and consistent in my code. Am I missing a consideration here? Should I prefer one version over the other for any technical reason?