ヘッダーガードとは?
ヘッダーガード(インクルードガードとも呼ばれる)は、ヘッダーを包んでその内容が最初にインクルードされたときだけ処理されるようにする、プリプロセッサディレクティブの集合です。最初のインクルード時に一意のマクロが定義され、以降はそれを確認して本体をスキップします。これにより、同じ宣言が1つの翻訳単位に二度現れるのを防ぎます。
なぜ重要か
ガードがないと、複数のインクルードパスを通じて取り込まれたヘッダーは再定義エラーを生み、ビルドを壊します。ヘッダーガードは、およそ自明でないすべてのヘッダーにとって基本的な安全策です。
関連ガイド
What Is a Preprocessor Directive?A preprocessor directive is an instruction handled before compilation that includes files, defines macros, or…
What Is a Translation Unit?A translation unit is the complete source seen by the compiler after preprocessing one source file, the unit…
What Is a Forward Declaration?A forward declaration tells the compiler a name exists and its type before its full definition, letting code…