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?

Effective C++ Third version, sample code compile list(4)Make sure that objects are initialized before they're used.

Last updated at Posted at 2018-06-19

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
#目的(purpose)

"Effective C++コンパイル記録は、コンパイラおよび対応標準により、コンパイルエラーMessageの違いを記録し、どのエラーが出たら、どの対応標準にすればエラーが少なくなるかを考察するための資料の第一歩です。
上記参照サイトにはコンパイルする仕組みを提供していない。簡易な台本(script)を作成する。

#成果(outcome)

##計画(plan)
(1)コンパイラの種類、対応標準の違いによってエラーの数が違う。
(2)同じエラー、警告であってもMessageの表現が違う。
(3) エラー、警告のMessageをネットで検索する際に役立てる。
(4)コード断片の役に立つまたは意味のあるコンパイル・リンク・実行エラーの取り方を検討する
(5)コード断片の役に立つまたは意味のある出力を検討する
##結果(result)現在整理中

  1. コンパイラによるエラー、渓谷の違い
  2. コンパイラによる必要なヘッダファイルの違い
  3. 参考にした資料

#算譜(source code)

ec4.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 4: Make sure that objects are initialized before they're used.";

/// From here "Effective C++

///In general, if you're in the C part of C++ and initialization would probably incur a runtime cost, initialization is not guaranteed to take place. If you cross into the non-C part of C++, things somethins change.

///The best way to deal with this is to always initialize your objects before you use them.

///Prefer initialization list...

///A static object is one that exists from the time it's constructed until the end of the program. Included are global objects, objects defined at namespace scope, objects declared static inside classes, objects declared static inside functions, and object declared static at file scope.

///Static objects inside functions are known as local static objects (Because they're local to a function), and the other kinds of static objects are known as non-local static objects.

///Static objects are automatically destroyed when the program exists.

///A translation unit is the source code giving rise to a single object file. It's basically a single source file, plus all of its #include files.

///The problem is the relative order of iniitalization of non-local static objects defined in different translatoin units is undefined.

class FileSystem {
public:
    std::size_t numDisks() const;
};
extern FileSystem tfs;

class Directory {
public:
    Directory(params);
}
Directroy::Directory(params) {
    std::size_t disks = tfs.numDisks();  // use the tfs object
}

Directory tempDir(params);  // Another non-local static object, tfs may be initialized!!!
///The problem is obvious, it's possible that tempDir is initialized before tfs.

///Use local static object!

class FileSystem {...};
FileSystem& tfs() {
    static FileSystem fs;
    return fs;
}
class Directory {...};
Directory::Directory(params) {
    std::size_t disks = tfs().numDisks();
}
Directory& tempDir() {
    static Directory td;
    return td;
}
///Objects will be initialized when you first call the functions.

///Things to Remember

///Manually initialize objects of built-in type, because C++ only sometimes initilizes them itself.
///In a constructor, prefer use of the member initialization list to assignment inside the body of the constructor. List data members in the initialization list in the same order they're declared in the class.
///Avoid initialization order problems across translatoin units by replacing non-local static objects with local static objects.
/// to here from "Effective C++"

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

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

cpa.sh
$ ../c1.sh ec4
$ clang++ ec4.cpp  -std=c++2a -Wall
ec4.cpp:37:15: error: unknown type name 'params'
    Directory(params);
              ^
ec4.cpp:39:1: error: use of undeclared identifier 'Directroy'; did you mean 'Directory'?
Directroy::Directory(params) {
^~~~~~~~~
Directory
ec4.cpp:35:7: note: 'Directory' declared here
class Directory {
      ^
ec4.cpp:39:22: error: unknown type name 'params'
Directroy::Directory(params) {
                     ^
ec4.cpp:35:7: error: 'A::Directory' cannot be defined in the result type of a function
class Directory {
      ^
ec4.cpp:39:12: error: constructor cannot have a return type
Directroy::Directory(params) {
           ^~~~~~~~~
ec4.cpp:43:19: error: unknown type name 'params'
Directory tempDir(params);  // Another non-local static object, tfs may be initialized!!!
                  ^
ec4.cpp:51:22: error: expected member name or ';' after declaration specifiers
class FileSystem {...};
                     ^
ec4.cpp:56:21: error: expected member name or ';' after declaration specifiers
class Directory {...};
                    ^
ec4.cpp:57:12: error: qualified reference to 'Directory' is a constructor name rather than a type in this context
Directory::Directory(params) {
           ^
ec4.cpp:58:17: error: expected '(' for function-style cast or type construction
    std::size_t disks = tfs().numDisks();
    ~~~~~~~~~~~ ^
ec4.cpp:59:2: error: expected ';' after top level declarator
}
 ^
 ;
11 errors generated.

#課題(agenda)

役に立つまたは意味のあるコンパイルエラーの取り方

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

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

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

文書履歴(document history)

ver. 0.10 初稿 20180620

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

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

Thank you very much for reading to the last sentence.

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

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?