テンプレートのインスタンス化とは?
テンプレートのインスタンス化は、コンパイラがそのパラメータを実際の型で置き換えることによって、汎用テンプレートから具体的な関数やクラスを生成するプロセスです。引数の一意な組み合わせごとに、独自の生成コードを持つ別々のインスタンス化が生じます。この処理はコンパイル時に行われ、それゆえテンプレートを多用するとビルドが長くなり得ます。
なぜ重要か
インスタンス化は実行時のディスパッチコストなしに型安全な汎用コードを提供しますが、個々の異なる使用ごとに、コンパイラとリンカが扱わねばならないコードが増えます。テンプレートを多用するコードでは、コンパイル時間とバイナリサイズの両方を大きく左右する要因です。
関連ガイド
What Is Monomorphization?Monomorphization compiles generic code into a separate specialized copy for each concrete type it is used wit…
What Is Type Erasure?Type erasure discards specific type parameters at or after compilation, leaving generic code working through…
What Is a Forward Declaration?A forward declaration tells the compiler a name exists and its type before its full definition, letting code…