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_ptr
constructor you're trying to use isexplicit
.Your first example
works because you're using direct initialization, which works with
explicit
constructors. 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_ptr
hasexpired
, the first solution will throwstd::bad_weak_ptr
, while the second option will construct an emptyshared_ptr
in that case.