3

それ自体がintパラメーターによってテンプレート化されているクラスCのテンプレートメソッドを特殊化したいと思います。

どうすればよいですか?

template <int D=1>
class C {
    static std::string  foo () { stringstream ss; ss << D << endl; return ss.str();}    
};

template <class X>
void test() { cout << "This is a test" << endl;}

template <>
template <int D>
void test<C<D> > () {cout << C<D>::foo() << endl;}

test()の特殊化は、「void test()の宣言に含まれるテンプレートパラメータリストが多すぎます」で失敗します。

4

2 に答える 2

2

関数テンプレートの部分的な特殊化は許可されていません。行う

template <int D>  
void test () {cout << C<D>::foo() << endl;}
于 2010-06-18T15:33:37.517 に答える
1

template<>の部分的な特殊化の最初のものは必要ありませんtest<C<D>>。さらに、クラステンプレートは部分的にしか特殊化できず、関数テンプレートは特殊化できません。このようなものが機能する可能性があります:

template <class X>
struct thing
{
    static void test() { cout << "This is a test" << endl;}
};

template <int D>
struct thing<C<D>>
{
    static void test() {cout << C<D>::foo() << endl;}
};

関数テンプレートが引数を取り、それを使用してテンプレート引数を推測した場合、次のようなオーバーロードを使用して同様の効果を得ることができます。

template <class X>
void test(const X&) { cout << "This is a test" << endl;}

template <int D>
void test(const C<D>&) {cout << C<D>::foo() << endl;}

test(3);  // calls first version
test(C<3>()); // calls second version
于 2010-06-18T15:26:22.933 に答える