Yes, this is clearly a mistake; the code should use copy instead of uninitialized_copy . Since T will usually be a trivial type, resource leaks are usually avoided, but it is still a mistake.
To answer your first question, itβs easier to write the initializer of the default instance constructor element by building an array, as this allows the copy constructor to use the same simple distribution method as the (n, m) constructor. It would be more efficient to allocate and embed an uninitialized region of memory, but this would complicate constructors and destructors that could be distracted from the point of the Matrix class. A more idiomatic way to write this would be with Matrix wrapping a dynamic array class (fixed size), for example dynarray (possibly message -C ++ 14 TS).
As Puppy pointed out, the copy constructor has a memory leak if its body throws away. This will happen whether copy or uninitialized_copy . Earlier, Straustrup said // simplified (no error handling) earlier in the class, so this can be excused.
I guess I would say how this error arose in the fact that Stroustrup uses uninitialized_copy elsewhere in the book, especially in its vector class. I do not have access to older versions, but I have a suspicion that this was added to replace the loop and try / catch block in the vector copy constructor, and the Matrix was modified similarly without full consideration.
It is not shown in Streustrup the list of errors for the 4th edition, but it may already know (the last page of corrections has been updated. November 2013). If you feel that you are sending it by email with this error, you should definitely do it!
ecatmur
source share