在设计时没有考虑到异常安全的方法/类中提供强有力的保证
Providing strong guarantees in methods/classes that were not designed with exception-safety in mind
我有一个设计问题。让我们首先说这个代码
struct Trial{
const double a;
Trial(double a_) : a(a_){}
};
int main(){
Trial t1(1.);
Trial t2(2.);
t1 = t2;
}
不编译,因为默认情况下编译器不会生成Trial::operator=
,因为Trial::a
是const
。这是非常明显的。
现在重点是,代码优先
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,除了完整的分配
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- 在设计时没有考虑到异常安全的方法/类中提供强有力的保证