1

See this code:

// copy-constructor
dumb_array(const dumb_array& other)
    : mSize(other.mSize),
      mArray(mSize ? new int[mSize] : 0),
{
    // note that this is non-throwing, because of the data
    // types being used; more attention to detail with regards
    // to exceptions must be given in a more general case, however
    std::copy(other.mArray, other.mArray + mSize, mArray);
}

Why Copy CTOR is considered non-throwing? What if new int[mSize] will throw std::bad_alloc as it is new without (nothrow) argument. Also std::copy and throw?

Community
  • 1
  • 1
Narek
  • 38,779
  • 79
  • 233
  • 389

1 Answers1

11

It isn't, for the reason you give (that new may throw).

The comment refers only to the std::copy. Assigning ints is never going to throw anything. Good thing, too, because that would leak mArray. That's probably why the comment is there.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I raised a question to understand what that answer says, and now I need to raise another question to understand what you guys say. :) Please explain in details. – Narek May 09 '16 at 19:28
  • 3
    @Narek: What more explanation do you need? You asked why the copy-ctor is considered non-throwing. It isn't. That's it! – Lightness Races in Orbit May 09 '16 at 19:30
  • Then how you understand the comment here: `// note that this is non-throwing, because of the data ....`? Maybe I misunderstood it and this is where my confusion comes from? – Narek May 09 '16 at 19:56
  • @Narek: I addressed that in my answer. The comment relates to the `std::copy` call, not to the entire copy-constructor. Note its position inside the constructor's body. – Lightness Races in Orbit May 09 '16 at 20:11