成员“交换”的原因功能不应该扔? [英] why a member "swap" function shouldn't throw?
问题描述
您好,
据说如果我实施交换成员函数,然后它
永远不应该抛出任何异常。但是,如果我实施交换非
成员函数,则限制不适用。有人可以告诉
我为什么?
谢谢,
Jess
Hello,
It is said that if I implement a "swap" member function, then it
should never throw any exception. However, if I implement "swap" non-
member function, then the restriction doesn''t apply. Can somebody tell
me why?
Thanks,
Jess
推荐答案
Jess< wd *** @ hotmail.comwrote:
Jess <wd***@hotmail.comwrote:
据说,如果我实施交换"成员函数,然后它
永远不应该抛出任何异常。但是,如果我实施交换非
成员函数,则限制不适用。有人可以告诉我/ b $ b我为什么?
It is said that if I implement a "swap" member function, then it
should never throw any exception. However, if I implement "swap" non-
member function, then the restriction doesn''t apply. Can somebody tell
me why?
我认为该限制也适用于非会员功能。
I think the restriction should apply with non-member functions as well.
6月15日,13日:21,Jess< w ... @ hotmail.comwrote:
On 15 Jun, 13:21, Jess <w...@hotmail.comwrote:
你好,
据说如果我实现了交换成员函数,然后它
永远不应该抛出任何异常。但是,如果我实施交换非
成员函数,则限制不适用。有人可以告诉我/ b $ b我为什么?
Hello,
It is said that if I implement a "swap" member function, then it
should never throw any exception. However, if I implement "swap" non-
member function, then the restriction doesn''t apply. Can somebody tell
me why?
我认为限制必须适用,无论是否是
是否b。b
结构A
{
;
T2 t2_;
};
无效掉期(A& l,A& r)>
{
swap(l.t1,r.t1);
swap(l.t2,r.t2);
}
和第二次交换抛出,然后你有一半交换对象的
。
问候
DS
I think that the restriction must apply regardless to whether it is
member or not.
consider
struct A
{
T1 t1_;
T2 t2_;
};
void swap(A & l, A & r)
{
swap(l.t1, r.t1);
swap(l.t2, r.t2);
}
and second swap throws, then you have
half swapped objects.
regards
DS
dasjotre写道:
dasjotre wrote:
void交换(A& l,A& r)
{
swap(l.t1,r.t1);
swap( l.t2,r.t2);
}
和第二次交换抛出,然后你有半个交换对象的
。
void swap(A & l, A & r)
{
swap(l.t1, r.t1);
swap(l.t2, r.t2);
}
and second swap throws, then you have
half swapped objects.
但是替代方案是什么?抑制任何错误并且在没有机会知道的情况下有一个错误的状态?
But what is the alternative? Suppressing any errors and have an
erroneous state without a chance of knowing?
这篇关于成员“交换”的原因功能不应该扔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!