Edited at

Effective C++ Third version, sample code compile list(2)Prefer consts, enums, and inlines to #defines.

More than 1 year has passed since last update.

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)


ec2.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 2: Prefer consts, enums, and inlines to #defines.";

/// From here "Effective C++

///Prefer compiler to preprocessor. #define will be handled be preprocessor.

#define ASPECT_RATIO 1.653
///The name may not get entered into the symbol table. Debug is hard!

///prefer using const

const double AspectRatio = 1.653;
///Two sepecial cases:

///pointer
namespace A { /// add for redefinition
const char* const authorName = "Scott Meyers"; // const pointer to const char
}/// add namespace A

// May prefer using std::string
namespace B { /// add for redefinition
const std::string authorName("Scott Meyers");
///class-specific constants
///Take it a member and make it static.

class GamePlayer {
private:
static const int NumTurns = 5; // constant declaration
int scores[NumTurns];
};

///For integral type (e.g., integers, chars, bools), as long as you don't take their address, you can declare them and use them without providing a definition.

///If you really want, no initial value is permitted at the point of definition.

const int GamePlayer::NumTurns; // definition of NumTurns`

///Older compiler may not accept the syntax above and above syntax only works for integral types.
} /// add namespace B
class CostEstimate {
private:
static const double FudgeFactor; // declaration, in hpp
};

const double CostEstimate::FudgeFactor = 1.35; // definition,in cpp

///But what if you want to know the value during compilatoin of the class (e.g. Define the length of an array)? Use "the enum hack".

class GamePlayer {
private:
enum {NumTurns = 5};
int scores[NumTurns];
};
///The enum hack is worth konwing about for several resons.

///In some ways more like a #define than a const does. No legal to take address, no storage.

///Lots of code employs it.

///What about using #define to implement functions?

#define CALL_WITH_MAX(a,b) f((a)>(b) ? (a):(b))
///Remember to parenthesize all the arguments in the macro body.

///And werid things may still happen.

void f(int a) { /// add f()definition
cout<<"MAX="<<a<<endl;///
}///

void g() { /// add g()
int a = 5, b = 0;
CALL_WITH_MAX(++a, b); // a is incremented twice
CALL_WITH_MAX(++a, b+10); // a is incremented once
///The number of times that a is incremented before calling f depends on what it is being compared with!
}///

///inline may be a better choice.

template<typename T>
inline void callWithMax(const T& a, const T& b) {
f(a > b ? a : b);
}
///Things to Remember

///For simple constants, prefer const objects or enums to #defines.

///For function-like marcos, prefer inline functions to #defines.
/// to here from "Effective C++"

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



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


cpa.sh

$ ../cpa.sh i2

$ clang++ i2.cpp -I./ -std=c++14 -Wall
i2.cpp:15:14: warning: unused variable 'AspectRatio' [-Wunused-const-variable]
const double AspectRatio = 1.653;
^
i2.cpp:20:19: warning: unused variable 'authorName' [-Wunused-const-variable]
const char* const authorName = "Scott Meyers"; // const pointer to const char
^
i2.cpp:32:9: warning: private field 'scores' is not used [-Wunused-private-field]
int scores[NumTurns];
^
i2.cpp:56:9: warning: private field 'scores' is not used [-Wunused-private-field]
int scores[NumTurns];
^
4 warnings generated.
MAX=7
MAX=10
Item 2: Prefer consts, enums, and inlines to #defines.
$ clang++ i2.cpp -I./ -std=c++17 -Wall
i2.cpp:15:14: warning: unused variable 'AspectRatio' [-Wunused-const-variable]
const double AspectRatio = 1.653;
^
i2.cpp:20:19: warning: unused variable 'authorName' [-Wunused-const-variable]
const char* const authorName = "Scott Meyers"; // const pointer to const char
^
i2.cpp:32:9: warning: private field 'scores' is not used [-Wunused-private-field]
int scores[NumTurns];
^
i2.cpp:56:9: warning: private field 'scores' is not used [-Wunused-private-field]
int scores[NumTurns];
^
4 warnings generated.
MAX=7
MAX=10
Item 2: Prefer consts, enums, and inlines to #defines.
$ clang++ i2.cpp -I./ -std=c++2a -Wall
i2.cpp:15:14: warning: unused variable 'AspectRatio' [-Wunused-const-variable]
const double AspectRatio = 1.653;
^
i2.cpp:20:19: warning: unused variable 'authorName' [-Wunused-const-variable]
const char* const authorName = "Scott Meyers"; // const pointer to const char
^
i2.cpp:32:9: warning: private field 'scores' is not used [-Wunused-private-field]
int scores[NumTurns];
^
i2.cpp:56:9: warning: private field 'scores' is not used [-Wunused-private-field]
int scores[NumTurns];
^
4 warnings generated.
MAX=7
MAX=10
Item 2: Prefer consts, enums, and inlines to #defines.

$ g++-8 i2.cpp -I./ -std=c++14 -Wall
MAX=7
MAX=10
Item 2: Prefer consts, enums, and inlines to #defines.
$ g++-8 i2.cpp -I./ -std=c++17 -Wall
MAX=7
MAX=10
Item 2: Prefer consts, enums, and inlines to #defines.
$ g++-8 i2.cpp -I./ -std=c++2a -Wall
MAX=7
MAX=10
Item 2: Prefer consts, enums, and inlines to #defines.


参考資料(reference)

Effective C++(項1〜5)解説

https://qiita.com/MoriokaReimen/items/58f183d421bb932cbbda

Effective C++ Third version, sample code compile list(まとめ

https://qiita.com/kaizen_nagoya/items/2092638c221e1033040c

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 初稿 20180619

ver. 0.11 参考文献追記 20180620

ver. 0.12 参考文献追記 20180623