2010-03-17から1日間の記事一覧

typenameキーワード

C++テンプレートテクニック 2-6 テンプレートの曖昧さ回避のために使用するらしい。 #include <iostream> using std::cout; using std::endl; struct ClassA { typedef int result; }; struct ClassB { static const int result = 5; }; int p; template<class T> void func (T)</class></iostream>…

コンストラクタテンプレート

C++テンプレートテクニック 2-5 コンストラクタテンプレートという名前がついてますが、要はクラステンプレートとメンバ関数テンプレートの合わせ技ですね。 関連 http://d.hatena.ne.jp/pknight/20090805/1249471903

テンプレート引数にポインタを渡す

C++テンプレートテクニック 2-4 テンプレート引数にはコンパイル時に値が確定しているグローバル変数や関数のポインタも渡すことができます。 #include <iostream> using std::cout; using std::endl; template<class T,void (T::*M)()> void func (T obj) { (obj.*M)(); } class CClass { publ</class></iostream>…

テンプレートと参照渡しを利用して配列の要素数を取得する

C++テンプレートテクニック 2-4 #include <iostream> using std::cout; using std::endl; template<class T,int N> int size(T (&)[N]) { return N; } int main () { int a[] = {3,4,5,6}; // 配列aの要素数を表示 cout << size(a) << endl; return 0; } $ main 4配列の参照渡しをした</class></iostream>…

配列のポインタ渡しと参照渡しの違い

C++

ポインタ渡しとは以下のような処理です。 #include <iostream> using std::cout; using std::endl; // ポインタ渡し void func(int *a) { cout << a[0] << endl; } int main () { int a[] = {3,4,5,6}; func(a); } また、以下のようにも書くことができます。 void func</iostream>…