Skip to content

Instantly share code, notes, and snippets.

@kkabdol
Last active February 12, 2016 16:54
Show Gist options
  • Save kkabdol/46aa8220c4f6763185c5 to your computer and use it in GitHub Desktop.
Save kkabdol/46aa8220c4f6763185c5 to your computer and use it in GitHub Desktop.
std::weak_ptr
#include <iostream>
#include <memory>
std::weak_ptr<int> gw;
void f()
{
// Has to be copied into a shared_ptr before usage
if (auto spt = gw.lock()) {
std::cout << *spt << "\n";
}
else {
std::cout << "gw is expired\n";
}
}
int main()
{
{
auto sp = std::make_shared<int>(42);
gw = sp;
f(); // 42
}
f(); // gw is expired
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment