lundi 7 décembre 2015

std::weak_ptr: lock or shared_ptr constructor?

There seem to be two ways to temporarily grab ownership of the resource pointed to by a weak_ptr:

  1. Use lock()
  2. Pass the weak_ptr to a shared_ptr constructor

Both of these produce a shared_ptr, with the lock returning a nullptr in the case that the weak_ptr is empty and the shared_ptr constructor throwing an exception.

So the question is: when should one or the other be used? Are there general guidelines or best practices related to this?

Aucun commentaire:

Enregistrer un commentaire