c++ - Boost::Math::Quaternion 中的异常防护习语

标签 c++ boost idioms

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/

相关文章:

c++ - 是否可以在 cmake 中更改库链接顺序

c++ - Boost 将派生类反序列化为基类指针

c++ - 有没有办法列出所有共享内存对象的名称?

list - 比计算单位列表长度更好的方法

go - 有没有一种惯用的方法来为 golang slice 做 "in"

c++ - cppcheck 指定要处理但不报告错误的文件目录

c++ - 我应该在哪里为我的 std::pair 特化定义运算符 >>?

c++ - 第三种加载动态链接库的方式?别针

c++ - boost asio无法识别计时器对象

coding-style - Clojure - 1 个函数的 2 个版本。哪个更地道?