Exception Safety in Containers

Exception Safety in Containers - Exception Safety in...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Exception Safety in Containers, Part 2 Jonathan Schilling, jls@sco.com In the previous issue we mentioned that one of the best improvements made to the standard over the last two meetings has been the requirement that standard library containers exhibit certain levels of exception safety. Here's an example of what that means, using lists and vectors (C++ Newsletter #015): #include <iostream> #include <list> using namespace std; class A { public: A(int i) { n = i; } #if CCTOR A(const A& a) { if (a.n < 6) n = a.n; else throw "too large"; } #endif int get() const { return n; } private: int n; }; int main() { list<A> la; la.push_back(A(0)); la.push_back(A(1)); typedef list<A>::iterator LI; try { for (int i = 2; i < 10; i++) { LI li = la.begin(); li++; la.insert(li, A(i)); } } catch (const char* s) { } // what does la contain now? for (LI i = la.begin(); i != la.end(); i++) cout << i->get(); cout << endl; } The list initially has two elements, and then we insert a series of elements before the second element position. When compiled without any user-supplied copy constructor, the output is:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 3

Exception Safety in Containers - Exception Safety in...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online