以前は次のコードを使用して、インクルード ファイルが複数回読み込まれないようにしていました。
#ifndef _STRING_
#include <string>
#endif
// use std::string here
std::string str;
...
このトリックは、書籍「API Design for C++」で説明されています。
私の同僚は、Visual Studio ではこれは必要ないと言っていました。文字列の実装ヘッド ファイルに が含まれている場合#pragma once
、コンパイル速度を向上させるためにインクルード ガードは必要ないからです。
あれは正しいですか?
元の本からの引用:
7.2.3 Redundant #include Guards
Another way to reduce the overhead of parsing too many include files is to add redundant preprocessor
guards at the point of inclusion. For example, if you have an include file, bigfile.h, that looks
like this
#ifndef BIGFILE_H
#define BIGFILE_H
// lots and lots of code
#endif
then you might include this file from another header by doing the following:
#ifndef BIGFILE_H
#include "bigfile.h"
#endif
This saves the cost of pointlessly opening and parsing the entire include file if you’ve already
included it.