1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AUTOSAR CountdownAdvent Calendar 2022

Day 5
この記事誰得? 私しか得しないニッチな技術で記事投稿!

Effective C++ Third version, sample code compile list(18) Make interfaces easy to use correctly and hrad to use incorrectly

Last updated at Posted at 2019-05-05

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)

ec18.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>
using namespace std;

const char * msg ="Item 18: Make interfaces easy to use correctly and hrad to use incorrectly";


/// From here "Effective C++


class Date {
public:
    Date(int month, int day, int year);
};

// May be misused
Date d(30, 3, 1995);

// can be prevented by introdution of new types
struct Day {
    explicit Day(int d) : val(d) {}
    int val;
};
///...
class Date {
public:
    Date(const Month& m, const Day& d, const Year& y);
};
Date(Month(3), Day(30), Year(1995));
//Restrict the values

class Month {
public:
    static Month Jan() { return Month(1); }
    static Month Feb() { return Month(2); }
  ///  ...
private:
    explicit Month(int m);
};
Date d(Month::Mar(), Day(30), Year(1995));
//Have your types behave consistently with the built-in types.

if (a*b = c)
//size() function in STL containers.

//To avoid resource leak:

Investment* createInvestment();

std::tr1::shard_ptr<Investment> createInvestment();  // force clients to store the return value in a tr1::shared_ptr
declare deleter in shared_ptr

// create a null shared_ptr with getRidOfInvestment as its deleter
std::tr1::shared_ptr<Investment> pInv(static_cast<Investment*>(0), getRidOfInvestment); 
//Cross-DLL problem: an object is created using new in one dynamically linked library but is deleted in a different DLL.

//shared_ptr prevents this problem.

//Things to Remember

//Good interfaces are easy to use correctly and hard to use incorrectly. You should strive for these characteristics in all your interfaces.
//Ways to facilitate correct use include consistency in interfaces and behavioral compatibility with built-in type.
//Ways to prevent errors include creating new types, restricting operations on types, constraining object values, and eliminating client resource management reponsiblities.
//tr1::shared_ptr supports custom deleters. This prevents the cross-DLL problem, can be used to automatically unlock mutexes (see Item 14), etc.

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

int main() {
  cout << msg << endl;
  return EXIT_SUCCESS;
}

編纂・実行結果(compile and go)

cpa.sh
$ ../cpa.sh ec16
$ clang++ ec16.cpp -I./ -std=c++14 -Wall
Item 16: Use the same form in corresponding uses of new and delete
$ clang++ ec16.cpp  -I./ -std=c++17 -Wall
Item 16: Use the same form in corresponding uses of new and delete
$ clang++ ec16.cpp  -I./ -std=c++2a -Wall
Item 16: Use the same form in corresponding uses of new and delete

$ g++-8 ec16.cpp  -I./ -std=c++14  -Wall
Item 16: Use the same form in corresponding uses of new and delete
$ g++-8 ec16.cpp  -I./ -std=c++17  -Wall
Item 16: Use the same form in corresponding uses of new and delete
$ g++-8 ec16.cpp  -I./ -std=c++2a  -Wall
Item 16: Use the same form in corresponding uses of new and delete

課題(agenda)

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

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

参考資料(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

一覧の一覧( The directory of directories of mine.) Qiita(100)
https://qiita.com/kaizen_nagoya/items/7eb0e006543886138f39

プログラマが知っていると良い「公序良俗」
https://qiita.com/kaizen_nagoya/items/9fe7c0dfac2fbd77a945

LaTeX(0) 一覧 
https://qiita.com/kaizen_nagoya/items/e3f7dafacab58c499792

自動制御、制御工学一覧(0)
https://qiita.com/kaizen_nagoya/items/7767a4e19a6ae1479e6b

Rust(0) 一覧 
https://qiita.com/kaizen_nagoya/items/5e8bb080ba6ca0281927

小川清最終講義、小川清最終講義(再)計画, Ethernet(100) 英語(100) 安全(100)
https://qiita.com/kaizen_nagoya/items/e2df642e3951e35e6a53

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.10 初稿 20181008
ver. 0.02 ありがとう追記  20230715

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?