I am able to convert back and forth inline.
std::shared_ptr<sfg::Notebook> mNotebook = ...;
std::weak_ptr<sfg::Notebook> weakNotebook(mNotebook);
std::shared_ptr<sfg::Notebook> strongNotebook(weakNotebook);
When I attempt to pass it to a method, I get the error:
"No viable conversion between std::weak_ptr<sfg::Notebook> to std::shared_ptr<sfg::Notebook>."
I am calling the method normally, and the method looks like:
onNotebookLeftClick(weakNotebook);
void onNotebookLeftClick(std::shared_ptr<sfg::Notebook> notebook) {
}
I am using OS X (10.10.2).
                        
The
shared_ptrconstructor you're trying to use isexplicit.Your first example
works because you're using direct initialization, which works with
explicitconstructors. You'd have the same problem if you used copy initialization insteadTo fix your error, do one of the following
or
Note that there's a difference between the two methods. If the
weak_ptrhasexpired, the first solution will throwstd::bad_weak_ptr, while the second option will construct an emptyshared_ptrin that case.