C++ Modules - why were they removed from C++0x? Will they be back later on?

Tomaka17 picture Tomaka17 · Aug 29, 2010 · Viewed 31.6k times · Source

I just discovered this old C++0x draft about modules in C++0x.

The idea was to get out of the current .h/.cpp system by writing only .cpp files which would then generate module files during compilation, which would then in turn be used by the other .cpp files.

This looks like a really great feature.

But my question is: why did they remove it from C++0x? Was it because of too many technical difficulties? Lack of time? And do you think they will consider working on it for an ulterior version of C++?

Answer

lanoxx picture lanoxx · Sep 2, 2014

C++ Modules draft (Technical Specification after C++17)

A draft and several updated revisions for the C/C++ module specification have been published by WG21 on open-std.org. I will link only to the latest documents here:

  • Working Draft, Extensions to C++ for Modules N4610 (October 2016).
  • Fourth revision published as P0142R0 (March 2016).
  • Wording for Modules published as P0143R2 (March 2016).
  • The clang team has published a second revision of their changes: P0273R1 (October 2016).

The following blog posts contain a summary of the standards meetings and in particular a summary of the current status of the modules draft:

Update: As explained in the Kona trip report that I linked to above, there are currently two competing proposals, one from Microsoft and one from Clang. The proposed solution from Microsoft does not allow to export Macros, while the solution from the Clang team would support exporting Macros. So far only Microsoft has formally submitted a draft for a module specification.

Module specification as proposed by Microsoft

Here is a quick overview of the most important concepts that this proposal contains. As its a draft this might possibly still change. The new modules standard will among other things consist of the following:

A module keyword to declare a module, multiple files can declare this to build one module (but for each module only one compilation-unit can contain an export {} section):

module M;

An import keyword to import modules, instead of import it might also be decided to use using module instead, so a new import keyword could be avoided.

import std.io;
import module.submodule;

An export syntax, which defines the public declarations that are part of this module, non-interface declarations that should not be exported as part of the module will be defined outside the export block. Declarations can be any kind of declaration in C/C++, that is, not only functions but also variables, structs, templates, namespaces and classes:

export {
    int f(int);
    double g(double, int);

    int foo;

    namespace Calc {
         int add(int a, int b);
    }        
}

void not_exported_function(char* foo);

An important change of modules will be that macros and preprocessor definitions will be local to modules and will not be exported. Thus macros do not have any impact on imported modules:

#define FILE "my/file"
import std.io;   //will not be impacted by the above definition

Its important note that the both the current preprocessor system and modules will be able to co-exist and headers can still be used for example to include macros.

For more detailed information I suggest to read the draft.

Clang Modules

Clang has been working on a modules implementation which can be found at the clang modules page. However clang does currently not implement a concrete syntax for modules, that is, none of the above mentioned syntax has been implemented by Clang. To explain this the page contains the following statement:

At present, there is no C or C++ syntax for import declarations. Clang will track the modules proposal in the C++ committee. See the section Includes as imports to see how modules get imported today.

The main part that is currently implemented by Clang is the "Module Map Language" which allows write module maps for existing code that still uses header files.

Macro Exports from Modules

As mentioned above it is still unclear if macro exports will be part of the final Modules TS. In P0273R1 the following syntax was proposed for the export of macros:

#export define MAX(A,B) ((A) > (B)) ? (A) : (B);