0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Effective C++ Third version, sample code compile list(11) Handle assignment to self in operator=

Last updated at Posted at 2018-06-22

Effective C++: 55 Specific Ways to Improve Your Programs and DesignsThird version by Scott Mayers
https://www.informit.com/store/effective-c-plus-plus-55-specific-ways-to-improve-your-9780321334879
https://www.amazon.com/dp/0321334876

Sample code from
Yuzhen11/Effective_CPP
https://github.com/Yuzhen11/Effective_CPP

#算譜(source code)

ec11.cpp
///Effective C++:  55 Specific Ways to Improve Your Programs and DesignsThird version by Scott Mayers
///https://www.informit.com/store/effective-c-plus-plus-55-specific-ways-to-improve-your-9780321334879
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

const char * msg ="Item 11: Handle assignment to self in operator=";

/// From here "Effective C++
namespace A {
  class Widget {
///...
  };

  void f() {
    Widget w;
///...
    w = w;
    int i=8;
    int j=9;
    int k=10;
    int a[k];
    int * px=&i;
    int * py=&j;
    a[i] = a[j];  // potential assignment to self
    *px = *py;  // potential assignment to self
//If you try to manage resources yourself, you can fall into the trap of accidentally releasing a resource before you're done using it.
  }
}

namespace B {
  class Bitmap {
/// ...
  };
  class Widget {
///    ...
  private:
    Bitmap* pb;
  };
//Here's an unsafe implementation:

  Widget& Widget::operator=(const Widget& rhs) {
    delete pb;
    pb = new Bitmap(*rhs.ph);
    return *this;
  }
//Traditional way to prevent this error:

  Widget& Widget::operator=(const Widget& rhs) {
    if (this == &rhs) return *this;
    delete pb;
    pb = new Bitmap(*rhs.ph);
    return *this;
  }
//This version still has exception trouble. What if new yield an exception, the Widget will end up holding a pointer to a deleted Bitmap.

//Happily, making operator= exeption-safe typically redners it self-assignment-safe too.

//A careful ordering of statements can yield exception-safe.

  Widget& Widget::operator=(const Widget& rhs) {
    Bitmap* pOrig = pb;  // remember the original pb
    pb = new Bitmap(*rhs.pb);
    delete pOrig;  // delete the original pb
    return *this;
  }
}
//Now, if new Bitmap throws an excpetion, pb remains unchanged. This code handle self-assignement too but with less efficient(delete and new).

//copy and swap
namespace D {
  class Widget {
    void swap(Widget& rhs);
  };
  Widget& Widget::operator=(const Widget& rhs) {
    Widget temp(rhs);
    swap(temp);
    return *this;
  }
// A variation, sacrifices clarity at the altar of cleverness.
  Widget& Widget::operator=(Widget rhs) {
    swap(rhs);
    return *this;
  }
}
//Things to Remember

//Make sure operator= is well-behaved when an object is assigned to itself. Technqiues include comparing addresses of source and target objects, careful statement ordering, and cpoy-and-swap.
//Make suret that any function operating on more than one object behaves correctly if two or more of the objects are the same.

/// to here from "Effective C++"

int main() {
  A::f();
  cout << msg << endl;
  return EXIT_SUCCESS;
}```
#編纂・実行結果(compile and go)

```shell-session:c1.sh
$ ../c1.sh ec11
$ clang++ ec11.cpp  -std=c++2a -Wall
ec11.cpp:43:17: error: 'operator=' is missing exception specification 'noexcept'
Widget& Widget::operator=(const Widget& rhs) {
                ^
                                             noexcept
ec11.cpp:36:7: note: previous declaration is here
class Widget {
      ^
ec11.cpp:45:26: error: no member named 'ph' in 'B::Widget'
    pb = new Bitmap(*rhs.ph);
                     ~~~ ^
ec11.cpp:50:17: error: 'operator=' is missing exception specification 'noexcept'
Widget& Widget::operator=(const Widget& rhs) {
                ^
                                             noexcept
ec11.cpp:36:7: note: previous declaration is here
class Widget {
      ^
ec11.cpp:53:26: error: no member named 'ph' in 'B::Widget'
    pb = new Bitmap(*rhs.ph);
                     ~~~ ^
ec11.cpp:62:17: error: 'operator=' is missing exception specification 'noexcept'
Widget& Widget::operator=(const Widget& rhs) {
                ^
                                             noexcept
ec11.cpp:36:7: note: previous declaration is here
class Widget {
      ^
ec11.cpp:76:17: error: 'operator=' is missing exception specification 'noexcept'
Widget& Widget::operator=(const Widget& rhs) {
                ^
                                             noexcept
ec11.cpp:73:7: note: previous declaration is here
class Widget {
      ^

#課題(agenda)
1)役に立つまたは意味のあるコンパイルエラーの取り方

2)役に立つまたは意味のある出力

3)役に立つまたは意味のある関数、クラスの呼び出し方

#参考資料(reference)

docker gnu(gcc/g++) and llvm(clang/clang++)
https://qiita.com/drafts/059874ea39c4de64c0f7
[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。
https://qiita.com/kaizen_nagoya/items/5f4b155030259497c4de
C++N4606 Working Draft 2016, ISO/IEC 14882, C++ standardのコード断片をコンパイルするためにしていること
https://qiita.com/kaizen_nagoya/items/a8d7ee2f2e29e76c19c1
コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)
https://qiita.com/kaizen_nagoya/items/74220c0577a512c2d7da
Clang/Clang++(LLVM) gcc/g++(GNU) コンパイラ警告等比較
https://qiita.com/kaizen_nagoya/items/9a82b958cc3aeef0403f
C++2003とC++2017でコンパイルエラーになるならない事例集
https://qiita.com/kaizen_nagoya/items/a13ea3823441c430edff
Qiitaに投稿するCのStyle例(暫定)
https://qiita.com/kaizen_nagoya/items/946df1528a6a1ef2bc0d
cpprefjpのdecltypeをコンパイル試験
https://qiita.com/kaizen_nagoya/items/090909af702f0d5d8a67
MISRA C++ 5-0-16
https://qiita.com/kaizen_nagoya/items/7df2d4e05db724752a74
C++ Templates Part1 BASICS Chapter 3. Class Templates 3.2 Use of Class Template Stack stack1test.cpp
https://qiita.com/kaizen_nagoya/items/cd5fc49106fad5a4e9ed
ISO/IEC TS 17961:2013 C Secure Coding Rules(1) All list(to be confirmed)
https://qiita.com/kaizen_nagoya/items/54e056195c4f11b850a1
C言語(C++)に対する誤解、曲解、無理解、爽快。
https://qiita.com/kaizen_nagoya/items/3f3992c9722c1cee2e3a
C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識
https://qiita.com/kaizen_nagoya/items/d89a48c1536a02ecdec9
'wchar.h' file not found で困った clang++ macOS
https://qiita.com/kaizen_nagoya/items/de15cd46d657517fac11
Open POSIX Test Suiteの使い方を調べはじめました
https://qiita.com/kaizen_nagoya/items/644d5e407f5faf96e6dc
MISRA-C 2012 Referenceに掲載している文献の入手可能性を確認
https://qiita.com/kaizen_nagoya/items/96dc8b125e462d5575bb
どうやって MISRA Example Suiteをコンパイルするか
https://qiita.com/kaizen_nagoya/items/fbdbff5ff696e2ca7f00
MISRA C まとめ #include
https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9
「C++完全理解ガイド」の同意できること上位10
https://qiita.com/kaizen_nagoya/items/aa5744e0c4a8618c7671
C++参考資料一覧
https://researchmap.jp/joub9b3my-1797580/#_1797580

#文書履歴(document history)

ver. 0.10 初稿 20180622

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?