- (クラス)テンプレートの特殊化がすべての関数を実装していることをどのように確認しますか?(現在、使用
mul
している場合にのみエラーメッセージが表示されます。) - traits1/traits2のintへの特殊化の違いは何ですか。どちらもテンプレートの特殊化だと思いましたが、traits2は受け入れず
static
、コンパイラエラーではなくリンカーエラーを出します。
。
#include <iostream>
template<typename T>
struct traits1{
static T add(T a, T b) { return a+b; } /* default */
static T mul(T a, T b); /* no default */
};
template<>
struct traits1<int> {
static int add(int a, int b) { return a*b; }
/* static int mul(int a, int b) missing, please warn */
};
template<typename T>
struct traits2{
static T add(T a, T b);
static T mul(T a, T b);
};
template<>
int traits2<int>::add(int a, int b) { return a*b; }
/* traits2<int>::mul(int a, int b) missing, please warn */
int main()
{
std::cout << traits1<int>::add(40, 2) << "\n";
// error: mul is not a member of traits1<int>
//std::cout << traits1<int>::mul(40, 2) << "\n";
std::cout << traits2<int>::add(40, 2) << "\n";
// error: undefined reference to traits2<int>::mul(int, int)
//std::cout << traits2<int>::mul(40, 2) << "\n";
return 0;
}