关于 C++ 的困惑20 个模块
在模块中,是否必须在 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模块作为一种语言功能没有“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 moduleM
), all declarations of it must appear in the purview of the moduleM
. 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++.
所以基本上有两个问题:
问题 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?
This official cppreference page on Modules (since C++20) explains it very clearly about it.
And if you are using one of the most common IDEs like Visual Studio then
this Overview of modules in C++ will further clarify your concepts.