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?
CodePudding user response:
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.
CodePudding user response:
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 .