Copy-and-swap

来源:互联网 发布:java final成员变量 编辑:程序博客网 时间:2024/05/01 19:18

为了在自定义类里重载一个异常安全(exception safe)的赋值操作符,创造了一个这样的习惯用语。也叫:Create-Temporary-and-Swap。
要想写健壮的C++代码,异常安全非常重要。
可以给异常安全分三级:
1> 基本安全:实现简单、代价小。应该作为常规手段。
2> 很安全:在任何场合都实现这一步不太可能。本例的赋值操作符重载算是一个。
3> 不抛出异常:可以看non-throwing swap。

所谓“Create-Temporary-and-Swap”,就是先申请新资源,用后再释放。
1> 在申请新资源的时候,要使用RAII原则。
2> 申请成功之后,使用non-throwing swap原则交换资源。

 

[cpp] view plaincopy
  1. class String {  
  2.     char * str;   
  3. public:  
  4.     String& operator = (String const &s)  
  5.     {  
  6.         String temp (s);    // copy-constructor -- RAII  
  7.         temp.swap (*this);  // non-throwing swap  
  8.         return *this;  
  9.     }  
  10.     void swap (String &s) throw ()  
  11.     {  
  12.         std::swap(this->str, s.str);  
  13.     }  
  14. };  

 

有时会加入对参数的判断:

 

[cpp] view plaincopy
  1. class String {  
  2.     char * str;  
  3. public:  
  4.     String& operator = (String const &s)  
  5.     {  
  6.         if (this != &s)  
  7.             String(s).swap (*this); // copy-constructor and non-throwing swap  
  8.         return *this;  
  9.     }  
  10.     void swap (String &s) throw ()  
  11.     {  
  12.         std::swap(this->str, s.str);  
  13.     }  
  14. };  

 

在函数内部的临时变量,其实可以省略,只要用传值的方式:

 

[cpp] view plaincopy
  1. String& operator = (String s) // pass-by-value  
  2. {  
  3.     s.swap (*this); // Non-throwing swap  
  4.     return *this;  
  5. }  

 

这种作法在某些情况下可以得到优化。
如果s绑定到lvalue,无优化,栈内会有临时对象被创建;如果绑定到rvalue,一般就会省略掉copy ctor的调用。

 

[cpp] view plaincopy
  1. String createString(); // a function that returns a String object.  
  2. String s;  
  3. s = createString();    // 这是rvalue的一例。使用传值的赋值操作符,可得到优化  

 

在C++0x标准中,这样的赋值操作符叫做“统一赋值操作符”,因为它合“拷贝赋值”、“移动赋值”为一体了。C++0x编译器一旦发现某个类有move ctor存在,总会对临时的rvalue进行优化的。
老编译器当然没有move ctor,但是能对赋值操作进行rvalue的优化,也算是有的一比。

0 0
原创粉丝点击