quote:Original post by Fruny
It works as a compilation speed optimization (like precompiled headers) where supported..
Yes, though you can have doublefold in compilation speed if you take it extreme.
a.hpp#ifndef A_HPP#define A_HPP...#endifb.hpp#ifndef B_HPP#define B_HPP#include "a.hpp"...#endifc.cpp#include "a.hpp"#include "b.hpp"...
In this case, compiling c.cpp will run as follow. Detected include of a.hpp. Open a.hpp. Check if A_HPP is defined. If not defined, include the rest of the contents within the inclusion guard. Ok control flows back to c.cpp. Detected include of b.hpp. Open b.hpp. Check if B_HPP is defined. If not defined, carry on the rest of the content,and in this case, include of a.hpp is detected again. Open a.hpp. Check if A_HPP is defined. It is defined now, so skip whatever is within the inclusion guard. Go back to c.cpp and process rest of files. Notice that there is still additional file access to a.hpp even though it has been included before. So, to furthur speed stuff up,
a.hpp#ifndef A_HPP#define A_HPP...#endifb.hpp#ifndef B_HPP#define B_HPP#ifndef A_HPP#include "a.hpp"#endif...#endifc.cpp#ifndef A_HPP#include "a.hpp"#endif#ifndef B_HPP#include "b.hpp"#endif...
In this second case, compiling c.cpp will run as follow. Check if A_HPP is defined. If not, detect include of a.hpp. Open a.hpp. Check if A_HPP is defined. If not defined, include the rest of the contents within the inclusion guard. Ok control flows back to c.cpp. Check if B_HPP is defined. If not defined, carry on the rest of the content,and in this case, detected include of b.hpp. Open b.hpp. Check if B_HPP is defined. Since B_HPP is not defined yet, carry on processing. At this time, however, check if A_HPP is defined. It is defined now, so skip the file opening. Go back to c.cpp and process rest of files. In projects where you have many inclusion and slow compile time, this might help speed up compilation.
This technique is described in Lakos'' Large Scale C++ Software Design if anyone is interested.