LoginSignup
2

プログラミング言語教育の母語方式を提唱

プログラミング言語教育のXYZ

youtube

1 コンパイラそのものを写経する
2 カーネルソースを写経する
3 本を10冊以上コンパイルする
4 1行づつ註釈(comment)にして編纂(compile or assemble)する

という方法を実践。

1は、PascalとC

2は、アセンブラとC

3は、アセンブラ、C, C++, JAVA
例:
C++N4606 Working Draft 2016, ISO/IEC 14882, C++ standard(1) Example code compile list
https://qiita.com/kaizen_nagoya/items/df5d62c35bd6ed1c3d43/

4は、アセンブラとC
例:
VZエディタ移植に当たって実施したことと成果
https://qiita.com/kaizen_nagoya/items/5551be98dcbed8f41949

現状、アセンブラとCには強いが、C++, JAVAには弱いプログラマ。

C++でTOPPERS/SSPを記述し直すことを検討。

CPU周りの記述をC++ Templateで記述できないかを検討。

コンパイラの勉強として、clang/clang++(LLVM)に馴染もうと。

別に、C++2003, C++2011, C++2017と言語規格の文書を辿る。

コンパイルエラーを辿って、言語学習になったか確認。

1 original

C++N4741(11)6.3.9 Template parameter scope [basic.scope.temp]p34
https://qiita.com/kaizen_nagoya/items/271138ed66f0bee8444c

p34.cpp
template<class T> class X : public Array<T> { /* ... */ };

全文は上記サイト

clang++

p34.cpp:26:36: error: no template named 'Array'; did you mean 'array'?
template<class T> class X : public Array<T> { /* ... */ };
                                   ^~~~~
                                   array
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/__tuple:223:64: note: 'array' declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                               ^
p34.cpp:26:36: error: too few template arguments for class template 'array'
template<class T> class X : public Array<T> { /* ... */ };
                                   ^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/__tuple:223:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                             ^

2. #define Array array

Arrayをarrayに定義してみる。

clang++
								   p34.cpp:26:36: error: too few template arguments for class template 'array'
template<class T> class X : public Array<T> { /* ... */ };
                                   ^
p34.cpp:12:15: note: expanded from macro 'Array'
#define Array array
              ^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/__tuple:223:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                             ^

3. include <array>

arrayをincludeする。

clang++
p34.cpp:26:36: error: too few template arguments for class template 'array'
template<class T> class X : public Array<T> { /* ... */ };
                                   ^
p34.cpp:12:15: note: expanded from macro 'Array'
#define Array array
              ^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/array:121:29: note: template is declared here
struct _LIBCPP_TEMPLATE_VIS array
                            ^

4. <T,i>にしてみる

2つ要素が要るらしい。なんでもいいから書いてみた。

clang++
p34d.cpp:26:44: error: use of undeclared identifier 'i'
template<class T> class X : public Array<T,i> { /* ... */ };
                                           ^
p34d.cpp:26:47: error: expected class name
template<class T> class X : public Array<T,i> { /* ... */ };
                                              ^
2 errors generated.

5. <T,size_t>にしてみる

宣言していない識別子はダメらしい。

clang++
p34a.cpp:26:44: error: template argument for non-type template parameter must be an expression
template<class T> class X : public Array<T,size_t> { /* ... */ };
                                           ^~~~~~
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/array:120:29: note: template parameter is declared here
template <class _Tp, size_t _Size>
                            ^
1 error generated.

6. <T,X>にしてみる

expressionじゃないと駄目だって。

clang++
p34.cpp:26:44: error: template argument for non-type template parameter must be an expression
template<class T> class X : public Array<T,X> { /* ... */ };
                                           ^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/array:120:29: note: template parameter is declared here
template <class _Tp, size_t _Size>
                            ^

ネットで検索
「Template argument for non-type parameter must be an expression」
https://stackoverflow.com/questions/37880517/template-argument-for-non-type-parameter-must-be-an-expression

7. constexpr size_t sz;

constexprすればいいのか?

clang++
p34.cpp:26:18: error: default initialization of an object of const type 'const size_t' (aka 'const unsigned long')
constexpr size_t sz;
                 ^
                 = 0

8 sz =0;

宣言で値が入っていないと駄目らしい。

clang++
$ ../cla.sh p34a
$ clang++ p34a.cpp -std=c++2a -Wall
C++N4741(11)6.3.9 Template parameter scope [basic.scope.temp]p34a.cpp

コンパイルエラーがなくなりました。

C++N4741(11)6.3.9 Template parameter scope [basic.scope.temp]p34
https://qiita.com/kaizen_nagoya/items/271138ed66f0bee8444c
p34a.cppがコンパイルエラーなし版

これが言語学習の経路として妥当かどうかは問わない。
母語は、意味のある言葉の繰り返しから生まれるとは限らない。
とにかく、言葉のやり取りだけでコンパイルエラーのないプログラムにたどり着けたことだけを記録。

言語学習は、目的が何かで任意の道のりで良い。

#参考文献(reference)

初学者がハマったエラーをまとめてみた【プログラミング学習107日目】
https://qiita.com/fuku_tech/items/0d4f2546cb4ee045c7f7

自己資料(self reference)

初めての CEDD(Compile Error Driven Design) 8回直してコンパイル。
https://qiita.com/kaizen_nagoya/items/9494236aa1753f3fd1e1

コンパイルエラーを記録するとよい理由7つ
https://qiita.com/kaizen_nagoya/items/85c0e92b206883140e89

C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識
https://qiita.com/kaizen_nagoya/items/d89a48c1536a02ecdec9

C言語(C++)に対する誤解、曲解、無理解、爽快。
https://qiita.com/kaizen_nagoya/items/3f3992c9722c1cee2e3a

Qiitaに投稿するCのStyle例(暫定)
https://qiita.com/kaizen_nagoya/items/946df1528a6a1ef2bc0d

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
[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。
https://qiita.com/kaizen_nagoya/items/5f4b155030259497c4de

docker gnu(gcc/g++) and llvm(clang/clang++)
https://qiita.com/drafts/059874ea39c4de64c0f7

文書履歴(document history)

ver. 0.10 初稿 20180504
ver. 0.11 空行を追記し表示訂正、URL3つ追記 20180505
ver. 0.12 参考文献追記 20180616
ver. 0.13 参考資料、はてなブックマーク追記 20190120

このエントリーをはてなブックマークに追加

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

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

Thank you very much for reading to the last sentence.

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

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
2