Skip to content

Instantly share code, notes, and snippets.

@Aposhian
Last active January 19, 2023 18:08
Show Gist options
  • Save Aposhian/7e6f2ac86ae2cc025fb3e334c13b243e to your computer and use it in GitHub Desktop.
Save Aposhian/7e6f2ac86ae2cc025fb3e334c13b243e to your computer and use it in GitHub Desktop.
Moving local into shared_ptr
#include <memory>
#include <iostream>
#include <vector>
struct Obj {
std::vector<int> data;
};
int main() {
std::shared_ptr<Obj> local_shared;
{
Obj local{{1 ,2, 3}};
local_shared = std::make_shared<Obj>(std::move(local));
}
std::cout << local_shared->data[0] << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment