问题描述
告诉编译器只包含一次文件有什么意义?默认情况下是否有意义?甚至没有理由多次包含一个文件吗?为什么不只是假设呢?
What's the point of telling the compiler specifically to include the file only once? Wouldn't it make sense by default? Is there even any reason to include a single file multiple times? Why not just assume it? Is it to do with specific hardware?
推荐答案
这里有多个相关问题:
-
为什么
#pragma一次
不会自动执行?
因为在某些情况下您想多次包含文件。
Why is
#pragma once
not automatically enforced?
Because there are situations in which you want to include files more than once.
为什么要多次包含文件?
Why would you want to include a file multiple times?
Several reasons have been given in other answers (Boost.Preprocessor, X-Macros, including data files). I would like to add a particular example of "avoid code duplication": OpenFOAM encourages a style where #include
ing bits and pieces within functions is a common concept. See for example this discussion.
确定,但是为什么不选择退出默认值?
因为该标准实际上没有指定。 #pragma
s定义为实现特定的扩展。
Ok, but why is it not the default with an opt-out?
Because it is not actually specified by the standard. #pragma
s are by definition implementation-specific extensions.
为什么有 #pragma曾经
尚未成为标准化功能(因为它被广泛支持)?
因为在文件中固定了相同文件与平台无关的方式实际上很难。 。
Why has #pragma once
not become a standardized feature yet (as it is widely supported)?
Because pinning down what is "the same file" in a platform-agnostic way is actually surprisingly hard. See this answer for more information.
这篇关于为什么不自动假定#pragma?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!