2012-01-26 12 views
1

私は非常に似ている2つの関数を持っており、それらからテンプレート関数を作りたいと思います。テンプレートの作成方法は?

void func1(vector<vector<vector<struct1_type> > > &struct1_x, const int &x, 
const int &y, struct2_type &struct2_y, list1<struct1_type> &l1) 

void func2(vector<vector<vector<struct1_type> > > &struct1_x, const int &x, 
const int &y, struct2_type &struct2_y, list2<struct1_type> &l2) 

の機能は同じことを行う...と異なる唯一のものは、リストを処理する方法には2つの異なるクラスである最後のパラメータです。

私は何も結果を出さず、エラーの犠牲になって何度も試してみました。相対的な初心者をお手伝いしてくれてありがとう!

答えて

0

これは、あなたの2つの関数宣言を一般化関数テンプレートを宣言する方法を次のとおりです。

template <typename L> 
void func1(vector<vector<vector<struct1_type> > > &struct1_x, const int &x, const int &y, struct2_type &struct2_y, L &q1) 

は、あなたが何を意味するのかということですか?

2

これはtemplate templateが発明されたものです。

template <template <typename> class list_type> 
void func1(vector<vector<vector<struct1_type> > > &struct1_x, 
      const int &x, 
      const int &y, 
      struct2_type &struct2_y, 
      list_type<struct1_type> &q1); 

注しかしテンプレートが正確と一致していること。たとえば、list_typeパラメータにはstd::listを使用できませんでした。テンプレートパラメータを1つは使用しないため、パラメータには含まれません。つまり、含まれる型とアロケータ型の2つです。

直接的でない、template templateソリューションを使用する方が簡単な場合があります。

template <typename list_type> 
void func1(vector<vector<vector<struct1_type> > > &struct1_x, 
      const int &x, 
      const int &y, 
      struct2_type &struct2_y, 
      list_type &q1); 

そして、ユーザーがlist1<struct1_type>をテンプレートパラメータとして指定するとします。これは、std::stack,std::queuestd::priority_queueとなります。

関連する問題