请参阅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);
}
为什么复制CTOR被视为非投掷?如果new int[mSize]
抛出std::bad_alloc
,因为它是新的而没有(nothrow)
参数,该怎么办?还std::copy并抛出?
答案 0 :(得分:11)
不是,因为您提供的原因(new
可能会抛出)。
评论仅指std::copy
。分配int
永远不会抛出任何东西。好的,因为那会泄漏mArray
。这可能就是评论的原因。