わんくま勉強会東京#3 の仕込み中。
template と generics の違いを見せるコードを書いてる。
http://blogs.wankuma.com/shuujin/archive/2005/12/11/19913.aspx
↑このコードをC++/CLIで書いてみる:
interface class IPrint {
void Print();
};
generic<typename T> where T : IPrint
ref class Printer {
public:
static void Print(T t) {
System::Console::WriteLine(L"以下のとおり出力します。");
t->Print();
}
};
ref class Greet : IPrint {
public:
void Print() {
System::Console::WriteLine("Hello, world");
}
};
int main() {
Printer<Greet^>::Print(gcnew Greet());
}
where T : IPrint がキモですな。
これがないとTに対してPrint()できることにならんからね。
さてさて、それでは template ref class てのをやってみる。
template<typename T>
ref class Printer {
public:
static void Print(T t) {
System::Console::WriteLine(L"以下のとおり出力します。");
t->Print();
}
};
ref class Greet {
public:
virtual void Print() {
System::Console::WriteLine("Hello, world");
}
};
int main() {
Printer<Greet^>::Print(gcnew Greet());
}
ほら、IPrintによる制約がなくなった。 template なのだなー。