Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
#include <memory>
int main() {
auto a = std::make_shared<int>(5);
auto b = a;
{
auto c = b;
std::cout << a.use_count() << ' ';
}
std::cout << a.use_count() << '\n';
}3 2
- Expecting
3 3:cis destroyed at the end of its block, and every destroyed owner lowers the count by one. - Expecting
2 2:ccopiesb, which shares the same int witha, souse_count()counts all three owners.
- At which line of this program is the int created by
std::make_shareddeleted? - How would a
std::weak_ptrmade fromachange the numbers this program prints?