Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

c++ - Is there a case where including the same header twice is actually helpful?

Creating header guards for my h/hpp files has always been standard practice for me, but I wonder, why is it even possible to include de same file twice? Is there a case where you actually need unprotected headers?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"Parameterized" header files can be used to simulate C++-ish-style templates in C. In such cases the header file will depend on a number of macros ("template parameters"). It will generate different code depending on the actual "value" of these macros.

So, the typical usage of such header would look as a sequence of "template parameter" macro definitions followed by the #include directive, followed by another sequence of "template parameter" macro definitions followed by the same #include, and so on.

https://stackoverflow.com/a/7186396/187690

When using this technique you will see header files without any include guards or header files with include guards that cover only a portion of the file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...