|
1年ぶりの更新 本当は8回でやめる予定だったけど 第9回はテンプレート関数、テンプレートクラスについての紹介 たとえば、今までは同じ機能を持つ関数は #include <iostream>
using namespace std;
int Add(int a,int b){
return a + b;
}
double Add(double a,double b){
return a + b;
}
int main(){
int resultInt = Add(5,10);
double resultDouble = Add(5.5,10.5);
cout << resultInt << endl;
cout << resultDouble << endl;
return 0;
}
のように2つ関数を書く必要がありました テンプレートを定義するには template <class T> のように書く必要があります template <typename T> では実際にみたほうが早いので #include <iostream>
using namespace std;
template <class T>
T Add(T a,T b){
return a + b;
}
int main(){
int resultInt = Add<int>(5,10);
double resultDouble = Add<double>(5.5,10.5);
cout << resultInt << endl;
cout << resultDouble << endl;
return 0;
}
ここでポイントはテンプレート関数を使用するときに Add<int>(5,10);
Add<double>(5.5,10.5);
のように使用時に型を指定していることです。 template <class T,class U> のように書きます #include <iostream>
using namespace std;
template <class T,class U>
T Add(T a,U b){
return a + b;
}
int main(){
double add = Add<double,int>(5.5,1);
cout << add << endl;
return 0;
}
使い方は Add<double,int>(5.5,1); のように2つ型を指定する必要があります。 テンプレート関数と同様にテンプレートクラスも定義することができます。 #include <iostream>
using namespace std;
template <class T>
class MyTemplate{
public:
// テンプレートクラスの関数
T Add(T a,T b);
// テンプレートクラスの関数(インライン)
T Sub(T a,T b){
return a - b;
}
// クラスのテンプレート関数(インライン)
template <class U>
U Mul(U a,U b){
return a * b;
}
// クラスのテンプレート関数(複数テンプレート)
template <class U,class S>
S Div(U a,S b);
};
// テンプレートクラスの関数実装
template <class T>
T MyTemplate<T>::Add(T a,T b){
return a + b;
}
// クラスのテンプレート関数(複数テンプレート)
template <class T>
template <class U,class S>
S MyTemplate<T>::Div(U a,S b){
return a/b;
}
int main(){
MyTemplate<int> temp;// クラス宣言時にTの型を決定する
int add = temp.Add(5,2);
double sub = temp.Sub(10,5);
float mul = temp.Mul<float>(2.5f,3.0f); // Uに埋め込むのでテンプレートクラスの型と関係ない
double div = temp.Div<float,double>(4.0f,1.5f); // U,S複数に型を埋め込み計算する
cout << add << endl;
cout << sub << endl;
cout << mul << endl;
cout << div << endl;
return 0;
}
ポイントは MyTemplate<int> temp;// クラス宣言時にTの型を決定する ほかについては特に注意する点はないかな temp.Mul<float>(2.5f,3.0f); テンプレートクラスのテンプレート関数の実装を分けて書くには // クラスのテンプレート関数(複数テンプレート)
template <class T>
template <class U,class S>
S MyTemplate<T>::Div(U a,S b){
return a/b;
}
のように2回templateを書きます テンプレートの欠点は関数やクラスを呼び出す部分でコンパイル時に型が決定されるため |