ゲームが作れるようになるまでがんばる日記

ゲーム制作のことを中心にゲームに関することを書いています

typeid演算子その2

以前、typeid演算子について学んだ(id:toburau:20071108)が、それはテンプレートクラスにも適用できる。

#include <iostream>
using namespace std;

template <class T> class Test
{
    T mValue;
public:
    Test(T n) { mValue = n; }
};

int main()
{
    Test<int> test0(123);
    Test<float> test1(1.0f);

    cout << typeid(test0).name() << endl;
    cout << typeid(test1).name() << endl;

    if (typeid(test0) == typeid(Test<int>)) cout << "test0 is Test<int>" << endl;
    if (typeid(test0) == typeid(test1)) cout << "same" << endl;
    else cout << "different" << endl;

    return 0;
}

実行結果

class Test<int>
class Test<float>
test0 is Test<int>
different