強力な例外安全保証は、例外が発生した場合、操作によってプログラムの状態が変更されないことを示しています。例外セーフなコピー代入を実装する洗練された方法は、コピー アンド スワップ イディオムです。
私の質問は次のとおりです。
非プリミティブ型を変更するクラスのすべての変更操作にコピー アンド スワップを使用するのはやり過ぎでしょうか?
パフォーマンスは、強力な例外安全性と本当に公正な取引ですか?
例えば:
class A
{
public:
void increment()
{
// Copy
A tmp(*this);
// Perform throwing operations on the copy
++(tmp.x);
tmp.x.crazyStuff();
// Now that the operation is done sans exceptions,
// change program state
swap(tmp);
}
int setSomeProperty(int q)
{
A tmp(*this);
tmp.y.setProperty("q", q);
int rc = tmp.x.otherCrazyStuff();
swap(tmp);
return rc;
}
//
// And many others similarly
//
void swap(const A &a)
{
// Non-throwing swap
}
private:
SomeClass x;
OtherClass y;
};