Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Correct typo in examples

...

Code Block
bgColor#FFcccc
langcpp
#include <new>
#include <utility>
 
struct S { /* ... */ }; // Has nonthrowing copy constructor
 
class T {
  int N;
  S *S1;
 
public:
  T(const T &RHS) : N(RHS.N), S1(RHS.S1 ? new S(*RHS.S1) : nullptr) {}
  T(T &&RHS) noexcept : N(RHS.N), S1(RHS.S1) { RHS.S1 = nullptr; }
  ~T() { delete S1; }
 
  // ...
 
  T& operator=(const T &RHS) {
    N = RHS.N;
    delete S1;
    S1 = new S(*RHS.S1);
    return *this;
  }
 
  T& operator==(T &&RHS) noexcept {
    N = RHS.N;
    S1 = RHS.S1;
    return *this;
  }
};

...

Code Block
bgColor#ccccff
langcpp
#include <new>
#include <utility>
 
struct S { /* ... */ }; // Has nonthrowing copy constructor
 
class T {
  int N;
  S *S1;
 
public:
  T(const T &RHS) : N(RHS.N), S1(RHS.S1 ? new S(*RHS.S1) : nullptr) {}
  T(T &&RHS) noexcept : N(RHS.N), S1(RHS.S1) { RHS.S1 = nullptr; }
  ~T() { delete S1; }

  // ...
 
  T& operator=(const T &RHS) {
    if (this != &RHS) {
      N = RHS.N;
      delete S1;
      try {
        S1 = new S(*RHS.S1);
      } catch (std::bad_alloc &) {
        S1 = nullptr; // For basic exception guarantees
        throw;
      }
    }
    return *this;
  }
 
  T& operator==(T &&RHS) noexcept {
    if (this != &RHS) {
      N = RHS.N;
      S1 = RHS.S1;
    }
    return *this;
  }
};

...

Code Block
bgColor#ccccff
langcpp
#include <new>
#include <utility>
 
struct S { /* ... */ }; // Has nonthrowing copy constructor
 
class T {
  int N;
  S *S1;
 
public:
  T(const T &RHS) : N(RHS.N), S1(RHS.S1 ? new S(*RHS.S1) : nullptr) {}
  T(T &&RHS) noexcept : N(RHS.N), S1(RHS.S1) { RHS.S1 = nullptr; }
  ~T() { delete S1; }

  // ...
 
  void swap(T &RHS) noexcept {
    using std::swap;
    swap(N, RHS.N);
    swap(S1, RHS.S1);
  }
 
  T& operator=(const T &RHS) noexcept {
    T(RHS).swap(*this);
    return *this;
  }
 
  T& operator==(T &&RHS) noexcept {
    if (&RHS != this) {
      N = RHS.N;
      S1 = RHS.S1;
    }
    return *this;
  }
};

...