クラス・テンプレートと個々のクラスとの間の関係は、 クラスと個々のオブジェクトとの間の関係に似ています。個々のクラスがオブジェクトのグループの構成方法を定義し、 一方、クラス・テンプレートがクラスのグループの生成方法を定義します。
template< template-parameter-list >
ここで、template-parameter-list は、次に示す種類のテンプレート・パラメーターの 1 つ以上を、
コンマで区切ったリストです。


template<class L, class T> class Key;
これにより、名前がクラス・テンプレート名として予約されます。 クラス・テンプレートのテンプレート宣言は、 すべてが、同じ型と同じ数のテンプレート引数を持っていなければなりません。 クラス定義を含む 1 つのテンプレート宣言だけが許可されます。


template<class L, class T> class Key { /* ... */};
template<class L> class Vector { /* ... */ };
int main ()
{
class Key <int, Vector<int> > my_key_vector;
// implicitly instantiates template
}

template<typename T> struct list {};
template<typename T>
struct vector
{
operator T() const;
};
int main()
{
// Valid, same as vector<vector<int> > v;
vector<vector<int>> v;
// Valid, treat the >> token as two consecutive > tokens.
// The first > token is treated as the ending delimiter for the
// template_parameter_list, and the second > token is treated as
// the ending delimiter for the static_cast operator.
const vector<int> vi = static_cast<vector<int>>(v);
}
括弧で囲んだ式は、区切り文字で区切られた式コンテキストです。template-argument-list 内でビット単位のシフト演算子を使用するには、括弧を使用して演算子を囲みます。次に例を示します。template <int i> class X {};
template <class T> class Y {};
Y<X<(6>>1)>> y; //Valid: 6>>1 uses the right shift operator

template<class T> class Vehicle
{
public:
Vehicle() { /* ... */ } // constructor
~Vehicle() {}; // destructor
T kind[16];
T* drive();
static void roadmap();
// ...
};
Vehicle<char> bicycle; // instantiates the template
コンストラクター、構成オブジェクト、およびメンバー関数 drive() は、 次のいずれかを指定してアクセスできます (標準ヘッダー・ファイル string.h が、プログラム・ファイルに含まれているとします)。
| コンストラクター |
|
| オブジェクト bicycle |
|
| 関数 drive() | char* n = bicycle.drive(); |
| 関数 roadmap() | Vehicle<char>::roadmap(); |