2012-01-06 9 views
4

からテンプレートを構成する私は、次のコードOM試してみましたが、それはcopmileしないtemplate<int, int>テンプレートの構築:テンプレート

からtemplate<int>を構成する方法です:

#include <iostream> 
template<int N, int M> 
struct A { enum E { n = N, m = M }; }; 

template<template<int> class C> 
struct B : public C<8> { }; 

int main(int argc, const char *argv[]) 
{ 
    typedef B< A<4> > T; 
    T b; 

    std::cout << T::n << std::endl; 
    std::cout << T::m << std::endl; 
    return 0; 
} 

エラー:

test3.cxx: In function ‘int main(int, const char**)’: 
test3.cxx:10:23: error: wrong number of template arguments (1, should be 2) 
test3.cxx:3:12: error: provided for ‘template<int N, int M> struct A’ 
test3.cxx:10:25: error: template argument 1 is invalid 
test3.cxx:10:28: error: invalid type in declaration before ‘;’ token 
test3.cxx:13:22: error: ‘T’ is not a class or namespace 
test3.cxx:14:22: error: ‘T’ is not a class or namespace 
+0

あなたがC++ 11を使用しています:これは、元のテンプレートに委譲することを右のアリティで新しいテンプレートを作成することです達成する

一つの方法は? –

+0

いいえ、違いはありますか? – Allan

+1

おそらく。テンプレートのエイリアスは、処理をスムーズにするために使用できますが、C++ 11にのみ存在します。 –

答えて

2

は、少なくともC++ 03でテンプレートテンプレートパラメータには、必要な引数の正確数を持っている必要があります。

template<int M> 
struct Hack : A<4, M> { }; 

... 

typedef B<Hack> T; 
+0

'typedef B >'を意味しますか? –

+0

@dark No. Bは、1つのintパラメーターを持つテンプレートを渡す必要があります。ハックはその要件にマッチします。 –

+0

私は分かりました、ありがとう、参照してください。 –

3

次のコードは4と8を出力します。私はあなたの意味を正しく推測してくれることを願っています。テンプレートテンプレート内のパラメータの数あなたが渡されたテンプレートの数と一致しませんでした。

#include <iostream> 
template<int N, int M> 
struct A { enum E { n = N, m = M }; }; 

template<template<int, int> class C, int Num> 
struct B : public C<Num, 8> { }; 

int main(int argc, const char *argv[]) 
{ 
    typedef B< A, 4 > T; 
    T b; 

    std::cout << T::n << std::endl; 
    std::cout << T::m << std::endl; 
    return 0; 
} 
関連する問題