Is there any situation where you wouldn't want include guards?

前端 未结 6 743
北海茫月
北海茫月 2021-01-11 22:08

I know why include guards exist, and that #pragma once is not standard and thus not supported by all compilers etc.

My question is of a different kind:<

6条回答
  •  逝去的感伤
    2021-01-11 22:32

    The problem with #pragma once, and the reason it is not part of the standard, is that it just doesn't always work everywhere. How does the compiler know if two files are the same file or not, if included from different paths?

    Think about it, what happens if the compiler makes a mistake and fails to include a file that it should have included? What happens if it includes a file twice, that it shouldn't have? How would you fix that?

    With include guards, the worst that can happen is that it takes a bit longer to compile.

    Edit: Check out this thread on comp.std.c++ "#pragma once in ISO standard yet?"

    http://groups.google.com/group/comp.std.c++/browse_thread/thread/c527240043c8df92

提交回复
热议问题