The following is toy code I am trying... I understand the first and second one. The first one give the ownership to _p
. The second one copies p
to _p
.
but I don't understand the third one...
What does std::move
of const shared_ptr &
mean? Thank you.
class P { };
class A {
public:
// first one
A(std::shared_ptr<P> &p, int) : _p(std::move(p))
{
std::cout << "1st Ctor: "
<< p.use_count() << ", " << _p.use_count() << std::endl;
}
// second one
A(const std::shared_ptr<P> &p, std::string) : _p(p)
{
std::cout << "2nd Ctor: "
<< p.use_count() << ", " << _p.use_count() << std::endl;
}
// third one
A(const std::shared_ptr<P> &p) : _p(std::move(p))
{
std::cout << "3rd Ctor: "
<< p.use_count() << ", " << _p.use_count() << std::endl;
}
private:
std::shared_ptr<P> _p;
};
int main()
{
{
std::shared_ptr<P> p = std::make_shared<P>();
A a(p, 1);
std::cout << "1. body: " << p.use_count() << std::endl;
}
std::cout << "-------------" << std::endl;
{
std::shared_ptr<P> p = std::make_shared<P>();
A a(p, "2");
std::cout << "2. body: " << p.use_count() << std::endl;
}
std::cout << "-------------" << std::endl;
{
std::shared_ptr<P> p = std::make_shared<P>();
A a(p);
std::cout << "3. body: " << p.use_count() << std::endl;
}
}
Result is:
$ ./a.out
1st Ctor: 0, 1
1. body: 0
-------------
2nd Ctor: 2, 2
2. body: 2
-------------
3rd Ctor: 2, 2
3. body: 2
(updated: adding comment to clarify which one is first one, second one, etc.)
&& p)` (which is a wee bit odd for a shared_ptr), or `A(std::shared_ptr
p)` (typical pattern for any sink parameter) would be the way to express the intent.
– Eljay May 15 '20 at 12:35