Download - 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」
![Page 1: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/1.jpg)
札幌C++勉強会#2
C++のマクロはどこまで関数をいじれるのか
~「Pythonで末尾再帰最適化」問題から~
H.Hiro(Maraigue)Twitter: @h_hiro_
![Page 2: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/2.jpg)
自己紹介
![Page 3: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/3.jpg)
H.Hiro(Maraigue)
![Page 4: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/4.jpg)
H.Hiro( ‿‿ )◕ ◕
![Page 5: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/5.jpg)
←これがアイコンです
![Page 6: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/6.jpg)
JR由仁駅の近くにあります
![Page 7: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/7.jpg)
大学院生(大学9年目)アルゴリズムの開発やってます普段はRubyの人ですでもC++もよく書きます他にはC#とかPythonとかC++は主に研究で利用Boostは全然使えてませんでも使い始めようと思ってます
![Page 8: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/8.jpg)
昨日はRuby勉強会で「ぽぽぽぽーん」言いすぎて
すみませんでしたhttp://www.slideshare.net/
maraigue/201172-ruby-rubygems
![Page 9: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/9.jpg)
最近、C++を書く機会が増えていて
![Page 10: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/10.jpg)
C++でももっと遊びたい
お年頃
![Page 11: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/11.jpg)
C++のマクロやテンプレートでもっと遊びたい
お年頃
![Page 12: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/12.jpg)
今日はそんな話をしたいと
思います
![Page 13: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/13.jpg)
お題
![Page 14: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/14.jpg)
末尾再帰最適化
![Page 15: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/15.jpg)
って何?
![Page 16: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/16.jpg)
関数の再帰呼び出しがその関数の末尾だけに
ある場合にはループに置き換えることで処理効率を良くできること
![Page 17: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/17.jpg)
言葉じゃ説明しにくいので
コードお見せしますPython: http://d.hatena.ne.jp/wasabiz/20110118/1295335821
Ruby: http://d.hatena.ne.jp/athos/20110119/p1
![Page 18: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/18.jpg)
これの面白いところは
![Page 19: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/19.jpg)
関数定義部が普通の再帰のコードなのにdef sum(n, acc=0):
if n == 0:
return acc
else:
return sum(n-1, acc+n)
![Page 20: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/20.jpg)
一言付け加えるだけで最適化されちゃったこと@tail_recursive # ←これ!注目!
def sum(n, acc=0):
if n == 0:
return acc
else:
return sum(n-1, acc+n)
![Page 21: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/21.jpg)
実用性はないかもしれないけど
ロマンがありますよね!
![Page 22: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/22.jpg)
じゃあこれをC++で行うには
どうすればよいか?
![Page 23: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/23.jpg)
札幌C++勉強会#2
C++のマクロはどこまで関数をいじれるのか
~「Pythonで末尾再帰最適化」問題から~
H.Hiro(Maraigue)Twitter: @h_hiro_
![Page 24: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/24.jpg)
※「コンパイラが最適化してくれるよ」ってツッコミはご遠慮頂けると幸いです
![Page 25: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/25.jpg)
理想的には
![Page 26: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/26.jpg)
元の関数がこんな感じならlong long sum
(long long count,
long long acc = 0){
if(count == 0) return acc;
return sum(count–1,
acc+count);
}
![Page 27: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/27.jpg)
↓これくらい楽に書ければいいよねTCO(long long sum)
(long long count,
long long acc = 0){
if(count == 0) return acc;
return sum(count–1,
acc+count);
}
![Page 28: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/28.jpg)
でもたぶん無理!
![Page 29: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/29.jpg)
じゃあ、どこまでできるか
やってみよう
![Page 30: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/30.jpg)
その前に:C++のマクロの基本
![Page 31: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/31.jpg)
#define TRUE 1…update(TRUE);
![Page 32: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/32.jpg)
#define TRUE 1…update(TRUE);// ↑コンパイル前に// update(1)に置き換えられる
![Page 33: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/33.jpg)
#define HOGE(a, b) (a*b)…HOGE(1+2, 3+4);
![Page 34: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/34.jpg)
#define HOGE(a, b) (a*b)…HOGE(1+2, 3+4);// ↑コンパイル前に// HOGE(1+2*3+4)に// 置き換えられる
![Page 35: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/35.jpg)
#define HOGE(a, b) (a*b)…HOGE(1+2, 3+4);// ↑コンパイル前に// HOGE(1+2*3+4)に ←あれ?// 置き換えられる
![Page 36: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/36.jpg)
#define HOGE(a, b) ((a)*(b))…HOGE(1+2, 3+4);// ↑コンパイル前に// HOGE((1+2)*(3+4))に// 置き換えられる
![Page 37: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/37.jpg)
では実際に以下の関数をいじろう
long long sum
(long long count,
long long acc = 0){
if(count == 0) return acc;
return sum(count–1,
acc+count);
}
![Page 38: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/38.jpg)
(ここからライブコーディング)
![Page 39: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/39.jpg)
ここまでできた!
![Page 40: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/40.jpg)
まだうまく出来ないか?
●引数の数によらない表記●「long long, sum」を 「long long sum」と書けないか?
![Page 41: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/41.jpg)
Boostだと黒魔術的な
マクロとかテンプレートとか多いので
![Page 42: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/42.jpg)
何とか出来るかも…
![Page 43: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/43.jpg)
しかし、私はそこまでの知識を
持ち合わせてない><
![Page 44: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/44.jpg)
協力求む!
![Page 45: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/45.jpg)
(ここからライブコーディング)
![Page 46: 2011.7.3 札幌C++勉強会#2「C++のマクロはどこまで関数をいじれるのか」](https://reader037.vdocuments.pub/reader037/viewer/2022100304/54939076ac7959412e8b4895/html5/thumbnails/46.jpg)
To BeContinued
(たぶん)