戻り値の形をテンプレートパラメータで変えるのはこないだやったので、今度は引数の形を変えてみる。
#include <iostream>
#include <string>
using namespace std;
enum class Type{
MEMORY_SIZE,
IP_ADDRESS
};
template <Type type>
struct Test_Traits{}; // empty class
template<>
struct Test_Traits<Type::MEMORY_SIZE>{
typedef unsigned int type;
};
template<>
struct Test_Traits<Type::IP_ADDRESS>{
typedef string type;
};
template <Type type>
void printParameter(typename Test_Traits<type>::type t){
cout << t << endl;
}
int main(){
printParameter<Type::MEMORY_SIZE>(300);
printParameter<Type::IP_ADDRESS>("192.168.0.2");
// printParameter<Type::MEMORY_SIZE>("192.168.0.2"); error
return 0;
}
あ、普通にできるね。