关于 C++ 的困惑20 个模块

发布于 2025-01-16 12:19:30 字数 82 浏览 4 评论 0原文

在模块中,是否必须在 .ixx 文件中创建声明并在 .cpp 文件中创建定义,还是将所有内容都放在 .ixx 文件中?关于模块还有什么我应该了解的吗?

In a module, do you have to both create declarations in a .ixx file and definitions in a .cpp file, or do you just put everything in a .ixx file? Is there anything else that I should know about modules?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2025-01-23 12:19:31

模块作为一种语言功能没有“ixx 文件”的概念。 MSVC 作为构建系统决定使用特定的扩展名,该扩展名默认指定文件是模块接口单元(分区或主单元)。但这对于标准甚至 MSVC 来说都不是必需的(您可以向构建系统单独指定特定文件是接口单元;构建系统只需要事先了解所有接口单元)。

声明和定义的要求非常简单。如果声明出现在模块 M 的权限范围内(即:不是模块 M 的任何模块单元的全局或私有片段的一部分),所有它的声明必须出现在模块M的权限中。请注意,定义也是声明。

另一个规则只是对有关内联定义需要对使用它们的代码可见的长期规则的修改。为此,如果您从模块接口单元导出内联声明(并且模板函数和变量是隐式内联的),则定义也必须位于该模块的接口单元之一中。并且它不能位于私有模块片段中。

除此之外,一切都或多或少是正常的 C++。

Modules as a language feature has no concept of an "ixx file". MSVC as a build system decided that it wanted to use a specific extension that by default designates that a file is a module interface unit (partition or primary). But that is not necessitated by either the standard or even MSVC (you can specify individually to the build system that a particular file is an interface unit; the build system simply needs to know about all of the interface units beforehand).

The requirements for declarations and definitions are pretty simple. If a declaration appears within the purview of a module M (ie: not part of the global or private fragment of any module unit for the module M), all declarations of it must appear in the purview of the module M. Note that a definition is also a declaration.

The other rule is just a modification of the long-standing rule about inline definitions needing to be visible by the code that uses them. To do this, if you export an inline declaration (and template functions&variables are implicitly inline) from a module interface unit, the definition must also be in one of that module's interface units. And it can't be in a private module fragment.

Other than that, everything is more or less normal C++.

幸福还没到 2025-01-23 12:19:30

所以基本上有两个问题:


问题 1

在模块中,是否必须在 .ixx 文件中创建声明并在 .cpp 文件中创建定义,还是只将所有内容都放在在 .ixx 文件中?


您可以使用单个接口文件 .ixx 创建模块,该文件导出名称并包含所有函数和类型的实现。这意味着这个 .ixx 接口可以包含函数定义和声明。您还可以将实现放入一个或多个单独的实现文件中,类似于 .h.cpp 文件的使用方式。 `


问题 2

关于模块还有什么我应该了解的吗?


  • 模块 (C++20 起) 对此解释得非常清楚。

  • 如果您使用的是最常见的 IDE 之一,例如 Visual Studio,那么
    C++ 模块概述将进一步阐明您的概念。

So basically there are two questions:


Question 1:

In a module, do you have to both create declarations in a .ixx file and definitions in a .cpp file, or do you just put everything in a .ixx file?


You can create a module with a single interface file .ixx that exports names and includes implementations of all functions and types. Meaning this .ixx interface can contain both the function definition and the declaration. You can also put the implementations in one or more separate implementation files, similar to how .h and .cpp files are used. `


Question 2:

Is there anything else that I should know about modules?


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文