Skip to content

Instantly share code, notes, and snippets.

@arielm
Created February 5, 2014 11:06
Show Gist options
  • Save arielm/8821289 to your computer and use it in GitHub Desktop.
Save arielm/8821289 to your computer and use it in GitHub Desktop.
Avoiding un-necessary shared_ptr copy when iterating over a container. Paste code into http://www.compileonline.com/compile_cpp11_online.php for testing.
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
int main()
{
vector<shared_ptr<string>> strings;
strings.push_back(make_shared<string>("foo"));
strings.push_back(make_shared<string>("bar"));
for (auto &s : strings)
{
cout << s.use_count() << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment