在设计时没有考虑到异常安全的方法/类中提供强有力的保证

Providing strong guarantees in methods/classes that were not designed with exception-safety in mind

本文关键字:强有力 方法 考虑到 安全 异常      更新时间:2023-10-16

我有一个设计问题。让我们首先说这个代码

struct Trial{
    const double a;
    Trial(double a_) : a(a_){}
};
int main(){
    Trial t1(1.);
    Trial t2(2.);
    t1 = t2;
}

不编译,因为默认情况下编译器不会生成Trial::operator=,因为Trial::aconst。这是非常明显的。

现在重点是,代码优先

struct Trial{
    const double a;
    Trial(double a_) : a(a_){}
};
struct MyClass : private Trial{
    MyClass(double a_) : Trial(a_), i(0) {};
    void wannaBeStrongGuaranteeMemberFunction(){
        MyClass old(i);
        bool failed = true;
        //... try to perform operations here
        if(failed)
            *this = old;
    }
    unsigned int i;
};
int main(){
    MyClass m1(1.);
    m1.wannaBeStrongGuaranteeMemberFunction();
}

我需要为从Trial派生的类的一些方法提供强大的异常安全性。这样的方法对无限系列的成员执行无限系列的操作(示例中为i),这使得"手动"恢复操作是不切实际的。因此,我决定最好对整个类执行一次复制,如果出现任何失败,则将其复制回来。

小括号,代码只是一个例子。在遗留的真实世界代码中,一切都要复杂得多。在本例中,只复制i是可以的,但实际代码中并非如此。此外,操作有多个(复杂的)执行路径,因此将它们"读取"为"事务"将是一件痛苦的事情。此外,我使用范围保护来实现这一点,因此在实际代码中可以正确地管理异常。

当然,由于*this = old行的原因,整件事并没有编译。

你将如何解决这个问题?

显而易见的答案是修改Trial,使其支持任务。除非如此,如果你要支援任务就是要提供强有力的保障,你可以实现你自己的赋值操作符,更好private,它忽略了基类;既然你知道两个基类将是相同的,不需要分配他们之间。

请注意,强担保通常涉及交换,而不是分配这并不能改变问题:你不能交换Trial的两个版本。你很可能已经类似于:

class MyClass : private Trial
{
    class Protected
    {
        bool myCommitted;
        MyClass* myOwner;
        MyClass myInstance;
    public:
        MyClass( MyClass& owner )
            : myCommitted( false )
            , myOwner( &owner )
            , myInstance( owner )
        {
        }
        ~MyClass()
        {
            if ( myCommitted ) {
                myOwner->swap( myInstance );
            }
        }
        MyClass& instance() { return myInstance; }
        void commit() { myCommitted = true; }
    };
public:
    void protectedFunc()
    {
        Protected p( *this );
        p.instance().unprotecedVersionOfFunc();
        //  ...
        p.commit();
    }

任何异常都将保持对象不变。(您可以,共当然,颠倒逻辑,对this进行修改,如果未提交则进行交换。)做事的好处这样,您还可以"撤消"内存中的任何更改分配等

最后:您真的想在Trial中使用const成员吗。实现这一点的通常方法是使a非常量但私有,并且只提供getter。这意味着Trial::a实际上是const,除了完整的分配