鉴于Jon Kalb用于解决Cargill Widget示例的强大的异常安全代码,是什么阻止了编译器重新组织操作,从而使代码不具有强大的异常安全性?是什么阻止编译器的优化重新排列强烈异常安全的代码?
#include <algorithm> // std::swap
template< typename T1, typename T2 >
class Cargill_Widget
{
public:
Cargill_Widget& operator=(Cargill_Widget const& r_other)
{
using std::swap;
T1 temp_t1(r_other.m_t1); // may throw
T2 temp_t2(r_other.m_t2); // may throw
/* The strong exception-safety line */
swap(m_t1, temp_t1); // no throw
swap(m_t2, temp_t2); // no throw
return *this;
}
private:
T1 m_t1;
T2 m_t2;
};
是它的“编译器不能改变观察到的行为”的规则?
参考:
- 乔恩·卡尔布的“异常安全的代码”演示幻灯片:http://www.exceptionsafecode.com/slides/esc.pdf