Is it ever useful to include a header file more than once in C or C++?
If the mechanism is never used, why would the compiler ever worry about including a file twice; if it really were useless, wouldn’t it be more convenient if newer compilers made sure every header is included only once?
Edit:
I understand that there are standard ways of doing things like include guards and pragma once, but why should you have to specify even that? Shouldn’t it be the default behavior of the compiler to include files only once?
Yes, it’s useful when generating code with the preprocessor, or doing tricks like Boost.PP does.
For an example, see X Macros. The basic idea is that the file contains the body of the macro and you
#definethe arguments and then#includeit. Here’s a contrived example:macro.xpp
file.cpp:
This also allows you to use
#ifand friends on the arguments, something that normal macros can’t do.