Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Stepanov-Regularity and Partially-Formed Objects vs. C++ Value Types


class Rect {
    int x1, y1, x2, y2;
public:

};

class Pen {
    class Private; // defined out-of-line
    Private *d;
public:

};

T a;
if (cond)
   a = b;
else
   a = c;

T a = (cond) ? b : c;

class Rect {
    int x1, y1, x2, y2;
public:
    Rect() = default;
};

int x;
Rect r;

int x = {};  // x == 0
Rect r = {}; // r == {0, 0, 0, 0}

class Pen {
    class Private; // defined out-of-line
    Private *d;
public:
    Pen() : d(nullptr) {} // inline
    ~Pen() { delete d; }  // out-of-line
};

Colour Pen::colour() const;

class Rect {
    int x1, y1, x2, y2;
public:
    Rect() = default;
    // compiler-generated copy/move special member functions are ok!
};

class Pen {
    class Private; // defined out-of-line
    Private *d;
public:
    Pen() noexcept : d(nullptr) {} // inline
    Pen(Pen &&other) noexcept : d(other.d) { other.d = nullptr; } // inline
    ~Pen() { delete d; }  // out-of-line
};

class Pen {
    class Private; // defined out-of-line
    Private *d;
public:
    Pen() noexcept : d(nullptr) {} // inline
    Pen(Pen &&other) noexcept : d(other.d) { other.d = nullptr; } // inline
    Pen &operator=(Pen &&other) noexcept                          // inline
      { Pen moved(std::move(other)); swap(moved): return *this; }
    ~Pen() { delete d; }  // out-of-line

    void swap(Pen &other) noexcept
      { using std::swap; swap(d, other.d); }
};

class Rect {
    static constexpr Rect emptyRect = {};
};

class Pen {
    static Pen none();
    static Pen solidBlackCosmetic();
};


6 Comments

2 - Feb - 2017

dyp

20 - Apr - 2017

Edward Welbourne

   Rect r;
   source >> r;
  Rect r[size];
  for (int i = 0; i < size; i++) r[i] = Rect(i, i, i+1, i+1);

15 - May - 2017

Marc Mutz

4 - Feb - 2018

alfC

Type& operator=(Type const& t){
assert(d==nullptr); 
...
}

5 - Feb - 2018

Marc Mutz

15 - May - 2019

alfC