请参阅这段代码
// 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);
}为什么复制者被认为是不扔的?如果new int[mSize]在没有(nothrow)参数的情况下抛出std::bad_alloc是新的怎么办?还有std:复制和扔?
发布于 2016-05-09 19:10:08
这并不是因为你给出的理由( new可能会抛出)。
评论只提到了std::copy。分配ints永远不会抛出任何东西。这也是件好事,因为这会泄露mArray。这可能就是为什么会有这样的评论。
https://stackoverflow.com/questions/37123500
复制相似问题