Chapter 22. Exception-Safe Class Design, Part 1: Copy Assignment

Difficulty: 7

Is it possible to make any C++ class strongly exception-safe, for example, for its copy assignment operator? If so, how? What are the issues and consequences? To illustrate, this Item explains and then solves the Cargill Widget Example.

  1. What are the three common levels of exception safety? Briefly explain each one and why it is important.

  2. What is the canonical form of strongly exception-safe copy assignment?

  3. Consider the following class:

    // Example 22-1: The Cargill Widget Example
    //
    class Widget
    {
    public:
      Widget& operator=( const Widget& ); // ???       // ...
    private:
      T1 t1_;
      T2 t2_;
    };
    

    Assume that any T1 or T2 operation might throw. Without changing the structure of the class, ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.