例えば:
template<unsigned number>
struct A
{
template<class T>
static void Fun()
{}
};
そして A<1>::Fun() を特化したい
template<>
A<1>::Fun<int>()
{
/* some code here. */
}
動作しません。どうやってするの?ありがとう。
例えば:
template<unsigned number>
struct A
{
template<class T>
static void Fun()
{}
};
そして A<1>::Fun() を特化したい
template<>
A<1>::Fun<int>()
{
/* some code here. */
}
動作しません。どうやってするの?ありがとう。
まず、関数の戻り値の型 ( void
) を指定するのを忘れていました。次に、 2 つ 必要ですtemplate<>
。1 つはクラス テンプレートを明示的に特殊化するため、もう 1 つはメンバー関数テンプレートを明示的に特殊化するためです。
したがって、これは正しい構文です。
template<> // Because you are explicitly specializing the A class template
template<> // Because you are explicitly specializing the `Fun()` member template
void A<1>::Fun<int>()
{
/* some code here. */
}