As many people have stated, the solution is to use C++11 features to avoid finally blocks. One of the features is unique_ptr
.
Here is Mephane's answer written using RAII patterns.
#include <vector>#include <memory>#include <list>using namespace std;class Foo{ ...};void DoStuff(vector<string> input){ list<unique_ptr<Foo> > myList; for (int i = 0; i < input.size(); ++i) { myList.push_back(unique_ptr<Foo>(new Foo(input[i]))); } DoSomeStuff(myList);}
Some more introduction to using unique_ptr with C++ Standard Library containers is here