Skip to content

Commit

Permalink
fix example code.
Browse files Browse the repository at this point in the history
  • Loading branch information
EzoeRyou committed Jul 19, 2018
1 parent d52d9db commit 7a31947
Show file tree
Hide file tree
Showing 13 changed files with 718 additions and 54 deletions.
4 changes: 2 additions & 2 deletions 003-guide-to-c++.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ int main()
x = x + 5 ;

// 15
std::cout << c ;
std::cout << x ;
}
~~~

Expand Down Expand Up @@ -700,7 +700,7 @@ void f()

ただし、戻り値の型については、具体的な型の代わりに`auto`を書くこともできる。その場合、return文で同じ型さえ返していれば、気にする必要はない。

~~~cpp
~~~c++
// void
auto a() { }
// int
Expand Down
2 changes: 1 addition & 1 deletion 005-the-restaurant-at-the-end-of-the-branch.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ int main()
{
std::cout << std::boolalpha ;
auto print = [](auto b)
{ std::cout << b << "\n"s } ;
{ std::cout << b << "\n"s ; } ;

print( true == true ) ; // true
print( true == false ) ; // false
Expand Down
4 changes: 2 additions & 2 deletions 007-standard-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main()
double mass = 73.0 ;

// BMIの計算
dublle bmi = mass / (height*height) ;
double bmi = mass / (height*height) ;

// BMIの出力
std::cout << "BMI=" << bmi << "\n"s ;
Expand Down Expand Up @@ -90,7 +90,7 @@ int main()
double mass = 80.0 ;

// BMIの計算
dublle bmi = mass / (height*height) ;
double bmi = mass / (height*height) ;

// BMIの出力
std::cout << "BMI=" << bmi << "\n"s ;
Expand Down
16 changes: 8 additions & 8 deletions 008-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ int f( int x ) { return x ; }

関数の定義は一度しか書けない。

~~~cpp
~~~c++
// 定義
void f() {}
// エラー、再定義
Expand All @@ -313,7 +313,7 @@ void f() {}

なぜ関数は宣言と定義とに別れているかというと、C++では名前は宣言しないと使えないためだ。

~~~cpp
~~~c++
int main()
{
// エラー
Expand Down Expand Up @@ -875,7 +875,7 @@ int main()
~~~cpp
int main()
{
std::cout << 4\t8\t12\n5\t10\t15"s ;
std::cout << "4\t8\t12\n5\t10\t15"s ;
}
~~~

Expand Down Expand Up @@ -1087,12 +1087,12 @@ int main()

変数もカンマで複数宣言できると知った読者は、以下のように書きたくなるだろう。

~~~cpp
~~~c++
int main()
{
for ( int a = 1, b = 1 ;
a <= 9 ;
+++a, ++b,
++a, ++b,
std::cout << "\n"s
)
std::cout << a*b << "\t"s ;
Expand All @@ -1106,7 +1106,7 @@ int main()
~~~cpp
int main()
{
bool b = true
bool b = true ;
// for文による変数宣言は使わない
for ( ; b ; b = false )
std::cout << "hello"s ;
Expand Down Expand Up @@ -1524,7 +1524,7 @@ int main()

負数と正数の違いを考えるのは面倒だ。ここでは正数を引数として与えると10進数から2進数へ変換した答えを返してくる魔法のような関数`solve`をすでに書き終えたと仮定しよう。我々はまだ関数`solve`を書いていないが、その問題は未来の自分に押し付けよう。

~~~cpp
~~~c++
// 1,0のみを使った10進数から
// 2進数へ変換する関数
int solve( int n ) ;
Expand Down Expand Up @@ -1723,7 +1723,7 @@ int main()

~~~cpp
void f() { } // gに戻る
vopd g() { f() ; } // mainに戻る
void g() { f() ; } // mainに戻る
int main() { g() ; }
~~~
Expand Down
2 changes: 1 addition & 1 deletion 009-vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main()
std::vector<int> vi ;

// 浮動小数点数型doubleの値を保持するvector
~std::vector<double> vd ;
std::vector<double> vd ;

// 文字列型std::stringの値を保持するvector
std::vector<std::string> vs ;
Expand Down
2 changes: 1 addition & 1 deletion 010-debug-printf.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ debug: v = { 1, 8, 2, 5, 6, 9, 4, 1, 7, }

大小比較も問題ないようだ。では最終的に見つけた最も小さい値は、本当に最も小さい値だろうか。

~~~cpp
~~~c++
// 最小値を探すループ
for ( std::size_t index = head+1 ; index != size ; ++index )
{
Expand Down
17 changes: 4 additions & 13 deletions 011-integer.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int octal = 0123 ;

~~~cpp
// 10進数で5
int binary = 0b1010
int binary = 0b1010 ;

// 0bと0Bは同じ
int a = 0B1010 ;
Expand Down Expand Up @@ -398,23 +398,14 @@ auto a = 123ll ;
// long long int
auto b = 123LL ;
// unsigned long long int
auto c = 123ull
auto c = 123ull ;
~~~

### short int型

`short int型``int型`より小さい範囲の値を扱う整数型だ。`long`, `long long`と同様に、`unsigned short int`型もある。単に`short`と書くと、`short int`と同じ意味になる。

整数リテラルでは末尾にs/Sと書くと`short int型`になる。

~~~cpp
// short int
auto a = 123s ;
// short int
auto b = 123S ;
// unsigned short int ;
auto c = 123us ;
~~~
整数リテラルで`short int`型を表現する方法はない。z2

### char型

Expand Down Expand Up @@ -631,7 +622,7 @@ int main()
int main()
{
int x = 123 ;
short y = static_cast<T>(x) ;
short y = static_cast<short>(x) ;
}
~~~

Expand Down
8 changes: 4 additions & 4 deletions 012-floating-point.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ int main()

変数aの値は1万、変数bの値は1万分の1だ。変数cの値はa+bで10000.0001となるはずだが結果はどうだろう。

~~~cpp
~~~
10000
0.0001
10000
~~~

変数cの値は10000.0001ではない。
変数cの値は10000.0001ではない。この謎は浮動小数点数を学べば明らかになる。

## 浮動小数点数リテラル

Expand Down Expand Up @@ -111,7 +111,7 @@ $$a \times 10^{b}$$
auto a = 1.23456e2 ;
auto b = 123456e-3 ;
auto c = 123.456e0 ;
auto d = 123.456E0
auto d = 123.456E0 ;
~~~

この文法は、`a``b`をe/Eで挟むことによって浮動小数点数の値を指定する。
Expand Down Expand Up @@ -237,7 +237,7 @@ int main()
bool b = NaN != 0.0 ;
bool c = NaN == NaN ;
bool d = NaN != NaN ;
bool e = Nan < 0.0 ;
bool e = NaN < 0.0 ;
}
~~~

Expand Down
8 changes: 4 additions & 4 deletions 013-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ C++では多くの名前は宣言と定義に分かれている。宣言と定
int plus_one( int x ) ;

// 関数の定義
inf plus_one( int x ) // 宣言部分
int plus_one( int x ) // 宣言部分
// 定義部分
// 関数の本体
{
Expand Down Expand Up @@ -150,7 +150,7 @@ int main()
}
// 定義
int plus_one( int x ) ;
int plus_one( int x )
{
return x + 1 ;
}
Expand Down Expand Up @@ -427,7 +427,7 @@ int g()

名前空間エイリアスを名前空間の中で宣言すると、宣言以降の名前空間内で有効になる。

~~~cpp
~~~c++
namespace ns {
namespace A { int x { } ; }
namespace B = A ;
Expand Down Expand Up @@ -753,7 +753,7 @@ void f()

名前空間も同じだ。

~~~cpp
~~~c++
// グローバル名前空間スコープ

namespace ns {
Expand Down
15 changes: 7 additions & 8 deletions 014-iterator.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ int main()

++x ; // xは1番目の要素を指す。

bool b1 = (x == y) ; // false
bool b2 = (x != y) ; // true
bool b3 = (x == y) ; // false
bool b4 = (x != y) ; // true
}
~~~

Expand Down Expand Up @@ -169,11 +169,10 @@ int main()
std::vector<int> v = {1,2,3,4,5} ;
auto i = std::end(v) ;

--i ; // 最後の要素を指す
*i ; // 5
++i ; 最後の次の要素を指す
*i ; // エラー

--i ; // 最後の要素を指す
*i ; // 5
++i ; // 最後の次の要素を指す
*i ; // エラー
}
~~~

Expand Down Expand Up @@ -541,7 +540,7 @@ int main()
for ( auto i = std::begin(v), j = std::end(v) ;
i != j ; ++i )
{
std::cout << i ;
std::cout << *i ;
}
}
~~~
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ retest : test-tool
test-tool : bin/sample-code-checker

bin/sample-code-checker : bin/sample-code-checker.cpp
g++ -D _ISOC11_SOURCE -std=c++14 --pedantic-errors -Wall -pthread -O2 -o bin/sample-code-checker bin/sample-code-checker.cpp
g++ -D _ISOC11_SOURCE -std=c++17 --pedantic-errors -Wall -pthread -O2 -o bin/sample-code-checker bin/sample-code-checker.cpp

book : docs/index.html

Expand Down
14 changes: 7 additions & 7 deletions bin/sample-code-checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ std::string create_temp_source_file( std::string sample_code )

bool compile_check_gcc( std::string const & file_name )
{
std::string command = std::string("g++ -fsyntax-only -D _ISOC11_SOURCE -std=c++1z --pedantic-errors -Wall -pthread -include /tmp/sample-code-checker/all.h ") + file_name ;
std::string command = std::string("g++ -fsyntax-only -D _ISOC11_SOURCE -std=c++17 --pedantic-errors -Wall -pthread -include /tmp/sample-code-checker/all.h ") + file_name ;

int result = system( command.c_str() ) ;

Expand All @@ -110,7 +110,7 @@ bool compile_check_gcc( std::string const & file_name )

bool compile_check_clang( std::string const & file_name )
{
std::string command = std::string("clang++ -fsyntax-only -D _ISOC11_SOURCE -std=c++1z -stdlib=libc++ --pedantic-errors -Wall -pthread -include-pch /tmp/sample-code-checker/all.h.pch ") + file_name ;
std::string command = std::string("clang++ -fsyntax-only -D _ISOC11_SOURCE -std=c++17 -stdlib=libc++ --pedantic-errors -Wall -pthread -include-pch /tmp/sample-code-checker/all.h.pch ") + file_name ;

int result = system( command.c_str() ) ;

Expand All @@ -123,9 +123,9 @@ bool compile_check_clang( std::string const & file_name )
bool compile_check( std::string const & file_name )
{
return
compile_check_gcc( file_name )
&&
compile_check_clang( file_name ) ;
compile_check_gcc( file_name ) ;
// &&
//compile_check_clang( file_name ) ;
}

// check if a given sample code is a well-formed C++ code fragment
Expand Down Expand Up @@ -188,9 +188,9 @@ bool prepare_compile()
precompiled_header.write( all_std_headers.c_str(), all_std_headers.size() ) ;
}

int r1 = std::system("g++ -D _ISOC11_SOURCE -std=c++1z --pedantic-errors -Wall -pthread -x c++-header -o /tmp/sample-code-checker/all.h.gch /tmp/sample-code-checker/all.h") ;
int r1 = std::system("g++ -D _ISOC11_SOURCE -std=c++17 --pedantic-errors -Wall -pthread -x c++-header -o /tmp/sample-code-checker/all.h.gch /tmp/sample-code-checker/all.h") ;

int r2 = std::system("clang++ -D _ISOC11_SOURCE -std=c++14 --pedantic-errors -Wall -pthread -x c++-header -o /tmp/sample-code-checker/all.h.pch /tmp/sample-code-checker/all.h") ;
int r2 = std::system("clang++ -D _ISOC11_SOURCE -std=c++17 --pedantic-errors -Wall -pthread -x c++-header -o /tmp/sample-code-checker/all.h.pch /tmp/sample-code-checker/all.h") ;

return r1 == 0 && r2 == 0 ;
}
Expand Down
Loading

0 comments on commit 7a31947

Please sign in to comment.