boost::math::quaternion
的实现(您可以浏览它 here )广泛使用注释为 //exceptionguard
的惯用语。例如:
template<typename X>
quaternion<T> & operator += (quaternion<X> const & rhs)
{
T at = a + static_cast<T>(rhs.R_component_1()); // exception guard
T bt = b + static_cast<T>(rhs.R_component_2()); // exception guard
T ct = c + static_cast<T>(rhs.R_component_3()); // exception guard
T dt = d + static_cast<T>(rhs.R_component_4()); // exception guard
a = at;
b = bt;
c = ct;
d = dt;
return(*this);
}
在此上下文中//异常防护
是什么意思?
最佳答案
如果您正在阅读这段代码,您应该对自己说“到底为什么要制作所有这些疯狂的临时对象,而不是仅仅这样做
a += static_cast<T>(rhs.R_component_1());
b += static_cast<T>(rhs.R_component_2());
c += static_cast<T>(rhs.R_component_3());
d += static_cast<T>(rhs.R_component_4());
结束了吗?”
这些注释是为了回答您的问题:临时变量存在,因此如果任何操作抛出异常,四元数不会被部分修改。 IE,加法赋值应该是原子的。
关于c++ - Boost::Math::Quaternion 中的异常防护习语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22031060/