c++ template-primer

98
11/12/13 関数型都市忘年会 1 C++ テンプレート入門 @hotwatermorning

Upload: kohsuke-yuasa

Post on 30-Jun-2015

2.111 views

Category:

Documents


0 download

DESCRIPTION

2011年 12/10 関数型都市忘年会

TRANSCRIPT

Page 1: C++ template-primer

11/12/13 関数型都市忘年会 1

C++ テンプレート入門

@hotwatermorning

Page 2: C++ template-primer

関数型都市忘年会 211/12/13

自己紹介

● @hotwatermorning● C++でプログラム書いてます。

Page 3: C++ template-primer

関数型都市忘年会 311/12/13

自己紹介

● Boost.勉強会 #6 札幌を開催しました!

Page 4: C++ template-primer

11/12/13 関数型都市忘年会 4

Introduction

Page 5: C++ template-primer

5関数型都市忘年会11/12/13

IntroductionIntroduction

What's

Benefit

How to use

● 今日のお話● C++テンプレート入門● C++のテンプレートという仕組みを

紹介します

Conclusion

Page 6: C++ template-primer

6関数型都市忘年会11/12/13

IntroductionIntroduction

What's

Benefit

How to use

● 主な対象者● C++や、Cライクな文法の言語を

触ったことがある人● テンプレートは使いこなしていない

けど興味がある人● テンプレートとか日常的に使いこな

してるけど、発表をひやかしたい人

Conclusion

Page 7: C++ template-primer

7関数型都市忘年会11/12/13

IntroductionIntroduction

What's

Benefit

How to use

〜 本日のレシピ 〜● What's the Template?

● テンプレートとは?● What's the Benefit?

● テンプレートを使うと何が嬉しい?● How to use the Template.

● テンプレートの使い方/書き方

Conclusion

Page 8: C++ template-primer

11/12/13 関数型都市忘年会 8

What's the Template?

Page 9: C++ template-primer

9関数型都市忘年会11/12/13

What's the Template?

● テンプレートとは?● C++の言語機能の一つ

● 1990年7月の会議でC++の規格に取り込まれた。

● 型を差し替え可能なクラスや関数を書くことが出来る仕組み。

● 型とデータ構造とアルゴリズムを分離できる

● →コードの再利用性が高い

Introduction

What's

Benefit

How to use

Conclusion

Page 10: C++ template-primer

10関数型都市忘年会11/12/13

What's the Template?

● 元々の要望は、コンテナの格納できる方をパラメータ化したいということから● 標準ライブラリには柔軟で効率のいいコン

テナが必要● intを格納する動的配列● 浮動小数点数を格納する線形リスト● intからstringへの連想配列、などなど

● C with Classes(C++の前身)の頃はマクロで対処していた

Introduction

What's

Benefit

How to use

Conclusion

Page 11: C++ template-primer

関数型都市忘年会 1111/12/13

What's the Template?

#define stackdeclare(TYPE) \class stack(TYPE) { \ TYPE *min_; \ TYPE *max_; \ TYPE *top_; \public: \ stack(TYPE)(int n); \ ~stack(TYPE)(); \ void push(TYPE x); \ TYPE pop(); \};

Page 12: C++ template-primer

関数型都市忘年会 1211/12/13

What's the Template?

#define stackimplement(TYPE) \stack(TYPE)::stack(TYPE)(int n) { \ min_ = top_ = new TYPE(n); \ max_ = min_ + n; \} \stack(TYPE)::~stack(TYPE)() { \ delete[] min_; \} \void stack(TYPE)::push(TYPE x) { \ assert(top_ != min_); \ *top_++ = x; \} \

こんな感じで行を結合しながら書いていく・・・

Page 13: C++ template-primer

関数型都市忘年会 1311/12/13

What's the Template?

#define name2(X,Y) X/**/Y

=> トークンペーストマクロ => 現在の仕様では X##Yとする

#define declare(CLASS,TYPE) \ name2(CLASS,declare)(TYPE)

#define implement(CLASS,TYPE)\ name2(CLASS,implement)(TYPE)

#define stack(TYPE) name2(stack,TYPE)

Page 14: C++ template-primer

14関数型都市忘年会11/12/13

What's the Template?

● ユーザーコードでは● declare(stack, long);のように宣言● implement(stack, long);のように定義して、

のように使う。

Introduction

What's

Benefit

How to use

Conclusion

stack(long) sl(1024);

sl.push(123L);long value = sl.pop();

stack(long) sl(1024);

sl.push(123L);long value = sl.pop();

Page 15: C++ template-primer

15関数型都市忘年会11/12/13

What's the Template?

● マクロの問題点

● マクロはただのテキスト処理● スコープの概念もない● かの有名なmin/maxマクロ(in windows.h)

● コンパイラサポートがない● 型安全ではない● 型推論も出来ない

Introduction

What's

Benefit

How to use

Conclusion

Page 16: C++ template-primer

16関数型都市忘年会11/12/13

What's the Template?

● マクロの問題点

● 文法がC++のソースと違う。厄介で面倒でバグのもと● 複数行のマクロを書こうと思うと末尾の行

の結合が必要● 引数をカッコで括る必要が有ったりなかっ

たり● 引数が何度も評価されてしまったり

Introduction

What's

Benefit

How to use

Conclusion

Page 17: C++ template-primer

関数型都市忘年会 1711/12/13

What's the Template?

#define mul2(a,b) (a*b)

void func1(){ int x = mul2(2, 3); //=> (2*3) => 6 OK!

int y = mul2(2+1, 3+1); //=> (2+1*3+1) => 6 !?

//=> mul2(a,b)は、((a)*(b))という形の      //定義じゃないといけない}

Page 18: C++ template-primer

18関数型都市忘年会11/12/13

What's the Template?

● マクロではなく、ちゃんと言語機能としてサポートしようという風になった。

Introduction

What's

Benefit

How to use

Conclusion

Page 19: C++ template-primer

19関数型都市忘年会11/12/13

What's the Template?

● Stroustrupが選んだ設計方針案● Smalltalk風

● 動的typingと継承を使う● Clu風

● 静的typingと、引数で型を指定できる仕組みを使う

● 前者はランタイムコストが大きく、また静的型付け言語であるC++のやり方と相容れない。

Introduction

What's

Benefit

How to use

Conclusion

Page 20: C++ template-primer

20関数型都市忘年会11/12/13

What's the Template?

● Stroustrupが選んだ設計方針案● Smalltalk風

● 動的typingと継承を使う● Clu風

● 静的typingと、引数で型を指定できる仕組みを使う

● 前者はランタイムコストが大きく、また静的型付け言語であるC++のやり方と相容れない。=> 後者を採用

Introduction

What's

Benefit

How to use

Conclusion

Page 21: C++ template-primer

21関数型都市忘年会11/12/13

What's the Template?

● Stroustrupが選んだ設計方針案● Stroustrup曰く

「理想的には、C++はCluの方法に基づき、ランタイムとスペース効率が良く、コンパイル時のオーバーヘッドも少ない仕組みを実現したい、また柔軟性は、Smalltalkのように大きくなければならない」(D&E第一版 15.2 P.431)

Introduction

What's

Benefit

How to use

Conclusion

Page 22: C++ template-primer

22関数型都市忘年会11/12/13

What's the Template?

● そうして出来上がったのが今のテンプレート

Introduction

What's

Benefit

How to use

Conclusion

Page 23: C++ template-primer

関数型都市忘年会 2311/12/13

What's the Template?

template<class T>class ClassA{ T value_;};

template<class T>int FunctionB(T arg){ arg.get_value();}

Page 24: C++ template-primer

24関数型都市忘年会11/12/13

What's the Template?

● そうして出来上がったのが今のテンプレート

● テンプレート化したクラス● => クラステンプレート

Introduction

What's

Benefit

How to use

Conclusion

template<class T>class ClassA{ T value_;};

template<class T>class ClassA{ T value_;};

Page 25: C++ template-primer

25関数型都市忘年会11/12/13

What's the Template?

● そうして出来上がったのが今のテンプレート

● テンプレート化した関数● => 関数テンプレート

Introduction

What's

Benefit

How to use

Conclusion

template<class T>int FunctionB(T arg){ arg.get_value();}

template<class T>int FunctionB(T arg){ arg.get_value();}

Page 26: C++ template-primer

26関数型都市忘年会11/12/13

What's the Template?

● こんな感じで使えるという例Introduction

What's

Benefit

How to use

Conclusion

Page 27: C++ template-primer

27関数型都市忘年会11/12/13

What's the Template?

● こんな感じで使えるという例

● こんな定義のクラステンプレート

Introduction

What's

Benefit

How to use

Conclusiontemplate< class T, Alloc = std::allocator<T>>class vector{

size_t size () const; T & front (); T const & front () const; //その他ごにょごにょ};

template< class T, Alloc = std::allocator<T>>class vector{

size_t size () const; T & front (); T const & front () const; //その他ごにょごにょ};

Page 28: C++ template-primer

28関数型都市忘年会11/12/13

What's the Template?

● こんな感じで使えるという例Introduction

What's

Benefit

How to use

Conclusion//int型の要素を持つvectorクラスstd::vector<int> is;

//char型の要素を持つvectorクラスstd::vector<char> cs;

//std::string型の要素をもつvectorクラスstd::vector<std::string> ss;

//なんらかのユーザー定義型を要素に持つ//vectorクラスstd::vector<SomeUserDefinedType> us;

//int型の要素を持つvectorクラスstd::vector<int> is;

//char型の要素を持つvectorクラスstd::vector<char> cs;

//std::string型の要素をもつvectorクラスstd::vector<std::string> ss;

//なんらかのユーザー定義型を要素に持つ//vectorクラスstd::vector<SomeUserDefinedType> us;

Page 29: C++ template-primer

29関数型都市忘年会11/12/13

What's the Template?

● こんな感じで使えるという例

● 全部使い方一緒!

Introduction

What's

Benefit

How to use

Conclusion

is.size();cs.size();ss.size();us.size();

is.size();cs.size();ss.size();us.size();

Page 30: C++ template-primer

30関数型都市忘年会11/12/13

What's the Template?

● こんな感じで使えるという例

● それぞれ指定した型で要素を取得できる!!

Introduction

What's

Benefit

How to use

Conclusion

is.front(); //(int &)cs.front(); //(char &)ss.front(); //(std::string &)us.front(); //(SomeUserDefinedType &)

is.front(); //(int &)cs.front(); //(char &)ss.front(); //(std::string &)us.front(); //(SomeUserDefinedType &)

Page 31: C++ template-primer

31関数型都市忘年会11/12/13

What's the Template?

● もうひとつの例

● 二つの変数が与えられたときに、比較して小さい方を返す。

Introduction

What's

Benefit

How to use

Conclusion

Page 32: C++ template-primer

32関数型都市忘年会11/12/13

What's the Template?

● minマクロ

● 関数で実装するなら・・・?

Introduction

What's

Benefit

How to use

Conclusion

#define min(a,b) ((a)<(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))

?? min(?? a, ?? b) { return a < b ? a : b;}

?? min(?? a, ?? b) { return a < b ? a : b;}

Page 33: C++ template-primer

33関数型都市忘年会11/12/13

What's the Template?

● マクロはあんまり良くないので、関数で書きたい

● でも、こんな汎用的な処理を色んな型のバージョンで分けて書いていられない(そもそもユーザー定義型にも対応したい)

Introduction

What's

Benefit

How to use

Conclusion

Page 34: C++ template-primer

34関数型都市忘年会11/12/13

What's the Template?

● 比較可能であることを表す、IComparableから派生したクラスの変数だけを受け取って、仮想関数compareで比較する?● POD型に対応できない。● 比較するだけで仮想関数の仕組みを使わな

くてはいけないのは嬉しくない。

Introduction

What's

Benefit

How to use

Conclusion

Page 35: C++ template-primer

35関数型都市忘年会11/12/13

What's the Template?

● 先程の??の型は(a < b)が定義されてる型なら何でもいい

● テンプレート化

● 型安全なまま、抽象度の高いプログラムが書ける

Introduction

What's

Benefit

How to use

Conclusion

template<class T>T const & min(T const &a, T const &b){ return a < b ? a : b; //中味はさっきと全く一緒でおk。}

template<class T>T const & min(T const &a, T const &b){ return a < b ? a : b; //中味はさっきと全く一緒でおk。}

Page 36: C++ template-primer

36関数型都市忘年会11/12/13

What's the Template?

● テンプレートには、型だけではなく、コンパイル時定数を使用することも出来る。

Introduction

What's

Benefit

How to use

Conclusion

template<int N>struct twice{ static const int value = N + N;};

template<int N>struct twice{ static const int value = N + N;};

Page 37: C++ template-primer

11/12/13 関数型都市忘年会 37

What's the Benefit?

Page 38: C++ template-primer

38関数型都市忘年会11/12/13

What's the Benefit?

● テンプレートがあることで嬉しいことはなにか?

● => テンプレートはコードの再利用性を高める

Introduction

What's

Benefit

How to use

Conclusion

Page 39: C++ template-primer

39関数型都市忘年会11/12/13

What's the Benefit?

● テンプレートはクラスと関数の型をパラメータ化する仕組み

● 型をパラメータ化することで、クラスと関数を型と分離して書くことが出来る。

Introduction

What's

Benefit

How to use

Conclusion

Page 40: C++ template-primer

40関数型都市忘年会11/12/13

What's the Benefit?

● 型をパラメータ化したコンテナ● std::vector, std::list, std::map,

std::queue, etc...● 型がAssignableであるなど、いくつ

かの要件を満たすものであれば、何でもこれらコンテナに渡すことが出来る。

Introduction

What's

Benefit

How to use

Conclusion

Page 41: C++ template-primer

41関数型都市忘年会11/12/13

What's the Benefit?

● 型をパラメータ化した関数● std::min/max, std::swap, std::find,

std::sort, etc...● std::minとstd::maxは、型が

operator<で比較可能であれば、どんな型でも使用することが出来る。● operator<が定義されていなくても、比較関

数を指定するバージョンを使うことで、std::minを使用できる。

Introduction

What's

Benefit

How to use

Conclusion

Page 42: C++ template-primer

42関数型都市忘年会11/12/13

What's the Benefit?

● 型をパラメータ化した関数● std::min/max, std::swap, std::find,

std::sort, etc...● std::swapは、型がAssignableである

なら、どんな型でもswapに渡して使うことが出来る。

Introduction

What's

Benefit

How to use

Conclusion

Page 43: C++ template-primer

43関数型都市忘年会11/12/13

What's the Benefit?

● 型をパラメータ化した関数● std::min/max, std::swap, std::find,

std::sort, etc...● std::find, std::sortなど● これらはSTLの`アルゴリズム'と呼ば

れる。

Introduction

What's

Benefit

How to use

Conclusion

Page 44: C++ template-primer

44関数型都市忘年会11/12/13

What's the Benefit?

● 型をパラメータ化した関数● std::min/max, std::swap, std::find,

std::sort, etc...● std::find, std::sortなど● イテレータという仕組みによって、具体的なコンテナなどでなく、抽象的な値の列(シーケンス)を扱えるようになっている。

Introduction

What's

Benefit

How to use

Conclusion

Page 45: C++ template-primer

45関数型都市忘年会11/12/13

What's the Benefit?

● アルゴリズムは値の列を辿って何かしたい

● => コンテナなどを直接触らないで、イテレータと呼ばれる小さなクラスに、コンテナから要素を参照する処理を委譲。

● => アルゴリズムは、イテレータをテンプレートにすることで、イテレータの要件を満たす型を受け付けるようにする。

Introduction

What's

Benefit

How to use

Conclusion

Page 46: C++ template-primer

46関数型都市忘年会11/12/13

What's the Benefit?

● => イテレータが用意出来れば、どんなコンテナや、値の列であっても、それをアルゴリズムに対して使用できるようになる。● ポインタもイテレータの要件を満たす● というより、C++のイテレータは、ポインタ

を参考にして作られた。

Introduction

What's

Benefit

How to use

Conclusion

Page 47: C++ template-primer

47関数型都市忘年会11/12/13

What's the Benefit?

● プログラムの中にはたくさんのデータ型と、たくさんのクラスと、たくさんの関数がある。

● とあるプログラムの中の型の数をi、クラスの数をj、そしてアルゴリズムの数をkで表すとする。

Introduction

What's

Benefit

How to use

Conclusion

Page 48: C++ template-primer

48関数型都市忘年会11/12/13

What's the Benefit?

● それら全てに対応するプログラムを書くとすると、i * j * k個の組み合わせに対応しないといけない。

Introduction

What's

Benefit

How to use

Conclusion

Page 49: C++ template-primer

49関数型都市忘年会11/12/13

What's the Benefit?

● テンプレートを使って、● 型とコンテナ、● 型とアルゴリズム、● コンテナとアルゴリズム

● を分離すれば、それぞれ、型をiだけ、コンテナをjだけ、アルゴリズムをkだけ用意すればいい。

● つまりi + j + kで済む。

Introduction

What's

Benefit

How to use

Conclusion

Page 50: C++ template-primer

50関数型都市忘年会11/12/13

What's the Benefit?

● オブジェクト指向では継承によって、クラス定義を再利用することが可能になった。

● C++のテンプレートでは、型をパラメータ化することで、クラス定義の再利用を可能にした。ついでにオブジェクト指向では不可能だった、関数の再利用も可能にした。

Introduction

What's

Benefit

How to use

Conclusion

Page 51: C++ template-primer

51関数型都市忘年会11/12/13

What's the Benefit?

● テンプレートを使うことで、ジェネリックプログラミングが可能になった。

● ジェネリックプログラミング。● 「型を決定していないクラスや関数

の断片を用意しておき、それらの組み合わせでプログラミングをする」

(C++テンプレートテクニック 第一版 P.51)

Introduction

What's

Benefit

How to use

Conclusion

Page 52: C++ template-primer

52関数型都市忘年会11/12/13

What's the Benefit?

● テンプレートを使用して嬉しいことは、これだけじゃない。

● クラスがテンプレート化出来るということは、さらに重要な意味を持つ。

● => メタ関数● => Template Meta Programming

Introduction

What's

Benefit

How to use

Conclusion

Page 53: C++ template-primer

53関数型都市忘年会11/12/13

What's the Benefit?

● このようなクラステンプレートが有ったときに、

● という型はint constになる。● charをテンプレート引数に渡せばchar const

に。std::stringならstd::string constに。

Introduction

What's

Benefit

How to use

Conclusion

template<class T>struct add_const { typedef T const type;};

template<class T>struct add_const { typedef T const type;};

add_const<int>::typeadd_const<int>::type

Page 54: C++ template-primer

54関数型都市忘年会11/12/13

What's the Benefit?

● add_constに型を渡すと、型にconstを修飾した型を得ることができる。

Introduction

What's

Benefit

How to use

Conclusion

template<class T>struct add_const { typedef T const type;};

template<class T>struct add_const { typedef T const type;};

Page 55: C++ template-primer

55関数型都市忘年会11/12/13

What's the Benefit?

● このようなクラステンプレートが有ったときに、

● という型はint *になる。● charをテンプレート引数に渡せばchar *

に。std::stringならstd::string *に。

Introduction

What's

Benefit

How to use

Conclusion

template<class T>struct add_pointer {

typedef T * type;};

template<class T>struct add_pointer {

typedef T * type;};

add_pointer<int>::typeadd_pointer<int>::type

Page 56: C++ template-primer

56関数型都市忘年会11/12/13

What's the Benefit?

● add_pointerに型を渡すと、型に*を修飾した型を得ることが出来る。

Introduction

What's

Benefit

How to use

Conclusion

template<class T>struct add_pointer {

typedef T * type;};

template<class T>struct add_pointer {

typedef T * type;};

Page 57: C++ template-primer

57関数型都市忘年会11/12/13

What's the Benefit?

● クラス名を関数名、テンプレート引数を引数、typeを戻り値の選択という風に捉えると、関数のように考えることが出来る。

Introduction

What's

Benefit

How to use

Conclusion

クラス名<テンプレート引数>::typeクラス名<テンプレート引数>::type

Page 58: C++ template-primer

58関数型都市忘年会11/12/13

What's the Benefit?

● クラス名を関数名、テンプレート引数を引数、typeを戻り値の選択という風に捉えると、関数のように考えることが出来る。

● 通常の関数と違い、型に関する操作が基本。

● => Template Meta Programming

Introduction

What's

Benefit

How to use

Conclusion

クラス名<テンプレート引数>::typeクラス名<テンプレート引数>::type

Page 59: C++ template-primer

59関数型都市忘年会11/12/13

What's the Benefit?

● これをこじらせると^H^H^H^H^H^H発展させるとどうなるか

Introduction

What's

Benefit

How to use

Conclusion

Page 60: C++ template-primer

関数型都市忘年会 6011/12/13

What's the Template?

namespace mpl = boost::mpl;typedef mpl::vector<int, std::string, user_defined_type>seq1;typedef mpl::list<int, std::string, user_defined_type>seq2;

// mpl::equal は、Sequence の比較を行うBOOST_MPL_ASSERT((mpl::equal< seq1, seq2 >));

//型のシーケンス(コンパイル時データ構造)を作って、その要素(型が要素!)を比較

Page 61: C++ template-primer

61関数型都市忘年会11/12/13

What's the Benefit?

● これをこじらせると^H^H^H^H^H^H発展させるとどうなるか

● こんなものも● http://d.hatena.ne.jp/tarao/20111101/

1320143278

「C++のテンプレートでラムダ計算と型推論」

Introduction

What's

Benefit

How to use

Conclusion

Page 62: C++ template-primer

62関数型都市忘年会11/12/13

What's the Benefit?

● その他、テンプレートを使うことで可能になる、便利なテクニック● ポリシークラス

– クラスの挙動をテンプレートで差し替える● パラメータ化継承

– 継承関係をテンプレートで差し替える● CRTP

– 静的多態● タグディスパッチ

– 静的オーバーロード● SFINAE

– 静的オーバーロード

Introduction

What's

Benefit

How to use

Conclusion

Page 63: C++ template-primer

63関数型都市忘年会11/12/13

What's the Benefit?

● ポリシークラス● クラスの挙動をテンプレートで差し

替える

Introduction

What's

Benefit

How to use

Conclusion

Page 64: C++ template-primer

関数型都市忘年会 6411/12/13

What's the Benefit?

template<class Policy>struct logger{

//なんかいろいろなコンストラクタなど

void output(char const *msg) { Policy::output(msg); }

//なんかいろいろなメンバ関数};

Page 65: C++ template-primer

関数型都市忘年会 6511/12/13

What's the Benefit?

strict cout_policy{ static void output(char const *msg) { std::cout << msg << std::endl; }};struct debugger_policy{ static void output(char const *msg ) { OutputDebugString(msg); }};

Page 66: C++ template-primer

66関数型都市忘年会11/12/13

What's the Benefit?

● ポリシークラス

● 標準出力にログが吐き出される

Introduction

What's

Benefit

How to use

Conclusion

void function_to_be_logged(){ logger<cout_policy> lg;

lg.output("logging message");}

void function_to_be_logged(){ logger<cout_policy> lg;

lg.output("logging message");}

Page 67: C++ template-primer

67関数型都市忘年会11/12/13

What's the Benefit?

● ポリシークラス

● デバッグ出力にログが吐き出される

Introduction

What's

Benefit

How to use

Conclusion

void function_to_be_logged(){ logger<debugger_policy> lg;

lg.output("logging message");}

void function_to_be_logged(){ logger<debugger_policy> lg;

lg.output("logging message");}

Page 68: C++ template-primer

68関数型都市忘年会11/12/13

What's the Benefit?

● loggerを継承したりすることなく、loggerの挙動を変更することが出来る

Introduction

What's

Benefit

How to use

Conclusion

Page 69: C++ template-primer

69関数型都市忘年会11/12/13

What's the Benefit?

● loggerは受け取りたいポリシーにoutputという名前のstaticメンバ関数があることだけしか要求していない

● こんなクラスもloggerのポリシーに渡すことが出来る。

Introduction

What's

Benefit

How to use

Conclusion

//自分で定義した、//outputスタティックメンバ関数を持つクラスstruct my_logger_policy;

//自分で定義した、//outputスタティックメンバ関数を持つクラスstruct my_logger_policy;

Page 70: C++ template-primer

70関数型都市忘年会11/12/13

What's the Benefit?

● policyはloggerが必要とする要件にしか依存していない。

● loggerもpolicyに求める要件の実装にしか依存していない。

● Non-Intrusive:非侵入的● 「トリから始まるnon-intrusiveness談義

http://togetter.com/li/33641

Introduction

What's

Benefit

How to use

Conclusion

Page 71: C++ template-primer

71関数型都市忘年会11/12/13

What's the Benefit?

● その他、テンプレートを使うことで可能になる、便利なテクニック

● 多分ここらへんが聞いてて一番面白くなるはずのところですが残念ながら資料ができていませんごめんなさい。

Introduction

What's

Benefit

How to use

Conclusion

Page 72: C++ template-primer

11/12/13 関数型都市忘年会 72

How to use the Template.

Page 73: C++ template-primer

73関数型都市忘年会11/12/13

How to use the Template.

● テンプレートの使い方/書き方● クラステンプレート● 関数テンプレート

Introduction

What's

Benefit

How to use

Conclusion

Page 74: C++ template-primer

74関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの使い方Introduction

What's

Benefit

How to use

Conclusion

Page 75: C++ template-primer

75関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの使い方Introduction

What's

Benefit

How to use

Conclusionstd::vector<int> vs;

//長ければtypedefしてtypedef std::vector<int> int_vector;int_vector vs2;

std::vector<int> vs;

//長ければtypedefしてtypedef std::vector<int> int_vector;int_vector vs2;

Page 76: C++ template-primer

76関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの使い方Introduction

What's

Benefit

How to use

Conclusion

//2引数を取るようなクラステンプレートには//カンマ区切りでテンプレート型を指定するtypedef std::pair<std::string, double>pair_t;

//2引数を取るようなクラステンプレートには//カンマ区切りでテンプレート型を指定するtypedef std::pair<std::string, double>pair_t;

Page 77: C++ template-primer

77関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの使い方Introduction

What's

Benefit

How to use

Conclusion

//入れ子になったテンプレートtypedef std::list<std::list<int> >//入れ子になったテンプレートtypedef std::list<std::list<int> >

Page 78: C++ template-primer

78関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの使い方

● 入れ子になったテンプレート引数を指定するとき、右の山括弧の間にスペースを空けないと、operator>>と解釈が曖昧になるので、スペースが必要

● C++11からスペース開けなくてもよくなった● C++03のコンパイラでも、スペース開けなく

てもいいものもある。

Introduction

What's

Benefit

How to use

Conclusion

//入れ子になったテンプレートtypedef std::list<std::list<int> >//入れ子になったテンプレートtypedef std::list<std::list<int> >

Page 79: C++ template-primer

79関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの書き方

● 普通のクラス定義の直前にtemplate<class T>のようにして、パラメータ化する型を書く

Introduction

What's

Benefit

How to use

Conclusion

template<class T>struct ParameterizedClass{ /*ごにょごにょ*/ };

template<typename T>struct ParameterizedClass{ /*ごにょごにょ*/ };

template<class T>struct ParameterizedClass{ /*ごにょごにょ*/ };

template<typename T>struct ParameterizedClass{ /*ごにょごにょ*/ };

Page 80: C++ template-primer

80関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの書き方

● Tには好きな型名を付ける● 型の指定はclass/typename どちらで

も良い

Introduction

What's

Benefit

How to use

Conclusion

template<class T>struct ParameterizedClass{ /*ごにょごにょ*/ };

template<typename T>struct ParameterizedClass{ /*ごにょごにょ*/ };

template<class T>struct ParameterizedClass{ /*ごにょごにょ*/ };

template<typename T>struct ParameterizedClass{ /*ごにょごにょ*/ };

Page 81: C++ template-primer

81関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの書き方

● クラス内部では、既知の型のようにテンプレートを使用できる。

Introduction

What's

Benefit

How to use

Conclusion

template<class T>struct ParameterizedClass{ T member_variable_; T const & get_value () const { return member_variable_; } void set_value (T const &new_value) { member_variable_ = new_value; }};

template<class T>struct ParameterizedClass{ T member_variable_; T const & get_value () const { return member_variable_; } void set_value (T const &new_value) { member_variable_ = new_value; }};

Page 82: C++ template-primer

82関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの書き方

● このままだと、default_params_tの利用者がdefault_params_tから、ParameterizedClassになんの型が渡されたかを取得する方法がない

Introduction

What's

Benefit

How to use

Conclusion

typedef ParameterizedClass<int>default_params_t;

typedef ParameterizedClass<int>default_params_t;

Page 83: C++ template-primer

83関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの書き方● もし、クラステンプレートに渡され

たテンプレート引数を、クラス外部からも取得できるようにするには、メタ関数を用いる。

Introduction

What's

Benefit

How to use

Conclusion

template<class T>struct ParametrizedClass{ typedef T value_type;};

template<class T>struct ParametrizedClass{ typedef T value_type;};

Page 84: C++ template-primer

84関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの書き方● これで、先程のdefault_params_tか

元々のクラステンプレートに渡された型を取得できました。● 余談ですが、このdefault_params_tは、引

数なしメタ関数と呼ばれるものです。詳しくはC++テンプレートメタプログラミング(デビッド・アブラハムズ, アレクセイ・グルトヴォイ)を参照してください

Introduction

What's

Benefit

How to use

Conclusion

default_params_t::value_type;default_params_t::value_type;

Page 85: C++ template-primer

85関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの書き方● 複数のテンプレート引数はカンマ区切りで指定する。

Introduction

What's

Benefit

How to use

Conclusion

template<class T1, class T2>struct ParameterizedClass2{ /*ごにょごにょ*/ };

template<class T1, class T2>struct ParameterizedClass2{ /*ごにょごにょ*/ };

Page 86: C++ template-primer

86関数型都市忘年会11/12/13

How to use the Template.

● クラステンプレートの書き方● もっとも後ろのテンプレート引数か

ら順に、デフォルト引数を指定できる

● 頻繁に指定される引数を省略できる

Introduction

What's

Benefit

How to use

Conclusion

template< class T1, class T2 = void, class T3 = void, class T4 = void>struct ManyParamClass{ /*ごにょごにょ*/ };

template< class T1, class T2 = void, class T3 = void, class T4 = void>struct ManyParamClass{ /*ごにょごにょ*/ };

Page 87: C++ template-primer

87関数型都市忘年会11/12/13

How to use the Template.

● 関数テンプレートの使い方Introduction

What's

Benefit

How to use

Conclusion

Page 88: C++ template-primer

88関数型都市忘年会11/12/13

How to use the Template.

● 関数テンプレートの使い方Introduction

What's

Benefit

How to use

Conclusion template<class T>T const & min( T const &a, T const &b){ return a < b ? a : b;}

template<class T>T const & min( T const &a, T const &b){ return a < b ? a : b;}

Page 89: C++ template-primer

89関数型都市忘年会11/12/13

How to use the Template.

● 関数テンプレートの使い方Introduction

What's

Benefit

How to use

Conclusion int const m = std::min(3, 4);//assert(m == 3);

//windows.hでminマクロが//定義されているときの//workaroundint const m = (std::min)(3, 4);//関数をカッコで括っても呼び出せる

int const m = std::min(3, 4);//assert(m == 3);

//windows.hでminマクロが//定義されているときの//workaroundint const m = (std::min)(3, 4);//関数をカッコで括っても呼び出せる

Page 90: C++ template-primer

90関数型都市忘年会11/12/13

How to use the Template.

● 関数テンプレートの使い方● 引数からテンプレートの型が推論で

きる場合は、テンプレート引数を指定する必要はない。

Introduction

What's

Benefit

How to use

Conclusion

Page 91: C++ template-primer

91関数型都市忘年会11/12/13

How to use the Template.

● 関数テンプレートの書き方Introduction

What's

Benefit

How to use

Conclusion template<class T>void func(T const &t){ t.get_value();}

template<class T>void func(T const &t){ t.get_value();}

Page 92: C++ template-primer

92関数型都市忘年会11/12/13

How to use the Template.

● 関数テンプレートの書き方

*残りの説明は未実装。。。

Introduction

What's

Benefit

How to use

Conclusion template<class T>void func(T const &t){ t.get_value();}

template<class T>void func(T const &t){ t.get_value();}

Page 93: C++ template-primer

11/12/13 関数型都市忘年会 93

Conclusion

Page 94: C++ template-primer

94関数型都市忘年会11/12/13

Conclusion

● C++のテンプレートはとても便利で、実行時効率を損なうこと無く、柔軟で再利用性の高いコードを書くことが出来る仕組みです。

Introduction

What's

Benefit

How to use

Conclusion

Page 95: C++ template-primer

95関数型都市忘年会11/12/13

Conclusion

● C++11になって、C++のテンプレートはより便利になります(可変長テンプレートなど)

● 普段C++を使っていて、テンプレートをあまり使ったことがない人がいたら、ぜひテンプレートに手を出してみてはいかがでしょうか。

Introduction

What's

Benefit

How to use

Conclusion

Page 96: C++ template-primer

96関数型都市忘年会11/12/13

Conclusion

● 参考文献/サイト● 「C++リファレンスマニュアル」

アジソンウェスレイ● 「C++の設計と進化」

ソフトバンクパブリッシング● 「C++テンプレートテクニック」

ソフトバンククリエイティブ● 「C++テンプレートメタプログラミング」

翔泳社● http://www.cplusplus.com/● http://www.sgi.com/tech/stl/index.html

Introduction

What's

Benefit

How to use

Conclusion

Page 97: C++ template-primer

11/12/13 関数型都市忘年会 97

Thank You!

ありがとうございました!

Page 98: C++ template-primer

98関数型都市忘年会11/12/13

ちなみに。● この資料でやり残してること。

● typenameキーワード● template限定子● CRTPなどの紹介● 関数テンプレートの使い方と型推

論の紹介

Introduction

What's

Benefit

How to use

Conclusion