Last active
February 12, 2016 16:54
-
-
Save kkabdol/46aa8220c4f6763185c5 to your computer and use it in GitHub Desktop.
std::weak_ptr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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