Why isn't #pragma once automatically assumed?

后端 未结 6 987
盖世英雄少女心
盖世英雄少女心 2020-12-13 03:24

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 m

6条回答
  •  悲哀的现实
    2020-12-13 03:47

    There are multiple related questions here:

    • 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 #includeing bits and pieces within functions is a common concept. See for example this discussion.

    • Ok, but why is it not the default with an opt-out?
      Because it is not actually specified by the standard. #pragmas are by definition implementation-specific extensions.

    • 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.

提交回复
热议问题