Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么阻止编译器';s优化以重新排序强异常安全代码?_C++_Compiler Optimization_Exception Safety - Fatal编程技术网

C++ 什么阻止编译器';s优化以重新排序强异常安全代码?

C++ 什么阻止编译器';s优化以重新排序强异常安全代码?,c++,compiler-optimization,exception-safety,C++,Compiler Optimization,Exception Safety,考虑到Jon Kalb解决Cargill小部件示例的强异常安全代码,是什么阻止编译器重新组织操作,从而使代码不具有强异常安全性 #include <algorithm> // std::swap template< typename T1, typename T2 > class Cargill_Widget { public: Cargill_Widget& operator=( Cargill_Widget const& r_other )

考虑到Jon Kalb解决Cargill小部件示例的强异常安全代码,是什么阻止编译器重新组织操作,从而使代码不具有强异常安全性

#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;
};
#包括

    你自己说过:编译器不能做任何 可能会改变可观察的行为。它必须采取行动 考虑到可能的例外情况。潜在的限制 因此,在重新排序时可能会产生重大负面影响 对优化的影响。实际上,有两种情况需要解决 考虑:编译器不知道程序中发生了什么 正在调用的函数,因此无法对其进行重新排序,或者 编译器确实有能力优化accross 翻译单位,在这种情况下,它通常能够 确定函数不会抛出(假设不会抛出),然后 因此,重新排序就好像它忽略了异常一样。还是不 如果引发异常可能导致
    可观察到的行为,如果有

    任何优化都必须保留代码的可证明属性。否则,任何代码都可以被一个不做任何事情并且速度极快的程序所取代。例外的影响是可证明属性的一部分