c++クラステンプレートの特例化

1218 ワード

#include <iostream>
#include <typeinfo>

using namespace std;

template <typename T1, typename T2, typename T3>
class A
{
 public:

  A(){cout << typeid(*this).name() << " construction" << endl;}
  ~A(){}

  T1 m_a1;
  T2 m_a2;
  T3 m_a3;
};

template <typename T1, typename T2>
class A<T1, T1, T2>
{
 public:

  A(){cout << typeid(*this).name() << " construction" << endl;}
  ~A(){}

  T1 m_a1;
  T2 m_a2;
};

template <typename T1>
class A<T1, T1, T1>
{
 public:

  A(){cout << typeid(*this).name() << " construction" << endl;}
  ~A(){}

  T1 m_a1;
};


int main(void)
{
 A<int, char, char *> a1;
 A<int, int, char *> a2;
 A<int, int, int> a3;

 return 0;
}

実行結果:
A construction A construction A construction