Confusion About C++ 20 Modules
Asked Answered
A

2

5

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?

Apophthegm answered 24/3, 2022 at 0:8 Comment(0)
G
6

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?


Geosynclinal answered 24/3, 2022 at 0:22 Comment(0)
A
4

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

Argybargy answered 24/3, 2022 at 0:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.