Skip to content

Instantly share code, notes, and snippets.

@jnewbery
Created April 18, 2020 15:42
Show Gist options
  • Save jnewbery/10ee24a5ce783932accddbe4347f110e to your computer and use it in GitHub Desktop.
Save jnewbery/10ee24a5ce783932accddbe4347f110e to your computer and use it in GitHub Desktop.
Passing shared pointers
#include <iostream>
#include <memory>
struct Base
{
Base() { std::cout << " Base::Base()\n"; }
~Base() { std::cout << " Base::~Base()\n"; }
};
struct TraceByVal
{
std::shared_ptr<Base> p;
TraceByVal(std::shared_ptr<Base> p_in) : p(std::move(p_in))
{
std::cout << " TraceByVal::TraceByVal()\n"
<< " t.p.get() = " << p.get()
<< ", t.p.use_count() = " << p.use_count() << '\n';
}
};
struct TraceByLVRef
{
std::shared_ptr<Base> p;
TraceByLVRef(std::shared_ptr<Base>& p_in) : p(std::move(p_in))
{
std::cout << " TraceByLVRef::TraceByLVRef()\n"
<< " t.p.get() = " << p.get()
<< ", t.p.use_count() = " << p.use_count() << '\n';
}
};
struct TraceByConstLVRef
{
std::shared_ptr<Base> p;
TraceByConstLVRef(const std::shared_ptr<Base>& p_in) : p(std::move(p_in))
{
std::cout << " TraceByConstLVRef::TraceByConstLVRef()\n"
<< " t.p.get() = " << p.get()
<< ", t.p.use_count() = " << p.use_count() << '\n';
}
};
struct TraceByRVal
{
std::shared_ptr<Base> p;
TraceByRVal(std::shared_ptr<Base>&& p_in) : p(std::move(p_in))
{
std::cout << " TraceByRVal::TraceByRVal()\n"
<< " t.p.get() = " << p.get()
<< ", t.p.use_count() = " << p.use_count() << '\n';
}
};
int main()
{
{
// By value
std::shared_ptr<Base> p = std::make_shared<Base>();
std::cout << "Created a shared Base\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
TraceByVal t(std::move(p));
std::cout << "Created a TraceByVal containing Base\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n'
<< " t.p.get() = " << t.p.get()
<< ", t.p.use_count() = " << t.p.use_count() << '\n';
}
{
// By lvalue reference
std::shared_ptr<Base> p = std::make_shared<Base>();
std::cout << "Created a shared Base\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
TraceByLVRef t(p);
std::cout << "Created a TraceByLVRef containing Base\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n'
<< " t.p.get() = " << t.p.get()
<< ", t.p.use_count() = " << t.p.use_count() << '\n';
}
{
// By const lvalue ref
std::shared_ptr<Base> p = std::make_shared<Base>();
std::cout << "Created a shared Base\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
TraceByConstLVRef t(std::move(p));
std::cout << "Created a TraceByConstLVRef containing Base\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n'
<< " t.p.get() = " << t.p.get()
<< ", t.p.use_count() = " << t.p.use_count() << '\n';
}
{
// By rvalue ref
std::shared_ptr<Base> p = std::make_shared<Base>();
std::cout << "Created a shared Base\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
TraceByRVal t(std::move(p));
std::cout << "Created a TraceByRVal containing Base\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n'
<< " t.p.get() = " << t.p.get()
<< ", t.p.use_count() = " << t.p.use_count() << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment