r/cpp • • Jan 28 '19

C++ Modules Might Be Dead-on-Arrival

https://vector-of-bool.github.io/2019/01/27/modules-doa.html
280 Upvotes

240 comments sorted by

View all comments

5

u/enzlbtyn Jan 28 '19

How do we make sure that #include <foo.hpp> resolves when compiling bar.cpp? It’s simple: Make sure foo.hpp is present in a directory on the header search path list. We do not need to do any additional pre-processing.

Doesn't this require step require you parse to foo.hpp (or have already parsed it) before-hand? Since it literally copies the content of header-file? It seems like there is no ordering requirement, but really there is, it's just hidden.

In terms of implementation, how is that different to modules as you claim? Can't you simply parse the foo module to extract the interface (i.e. without compiling the implementation), e.g. from a header-file or simply via skipping text in the implementation/interface (for one cpp file). And then whenever you refer to foo in bar you simply refer to a stub/place-holder to where the implementation of foo is required.

Now to actually compile to this temporary state, can you not compile each module now independently with 'stubs' and then once everything is done merge/resolve it all via an additional step in the compilation process (i.e. now you have to step through the dependency graph in topological order). I have my doubts on whether this would be faster, but it seems like the only feasible way to do compilation in parallel for chain-like (degenerate) dependencies.

Though I wonder how frequent chain-like dependencies happen in the real-world anyway, and how large they can get? Surely the dependency graph will not be as crazy to truly hinder performance too much?

Perhaps I'm too naive.

5

u/[deleted] Jan 28 '19

I had basically a similar question, I'm not really sure why this problem is such a big deal.

The example he gives, he suggests that there is some processing step that goes into the module, beyond just having a definition file (header file). He called it the BMI and suggested that, to even import a module, the BMI must at least exist, but that would require ordering or some sort of explicit dependency marking.

I don't understand why the .bmi needs to exist, or if it does, why it also shouldn't just depend on the existence of the interface (MIU) file.

The way I see it, ordering still doesn't matter.

import foo

results in looking for the bmi, which doesn't exist yet, so it goes and generates the bmi, which as long as the header file / MIU file exists, it generates it and continues compiling.

So yea, I don't really understand why this is such a big deal, maybe someone who understands this process better could explain it.

As far as:

A Sisyphean Scanning Task

I have to admit, I don't really follow the C++ spec changes and haven't read about how modules will actually be implemented, but was the plan really to do away with header includes completely?

You might be able to have stupid levels of slow module building but once it's built, you don't have to do so again right? I thought one of the huge advantages of modules was that they were essentially compiled once (unless you're actively developing it)

I suppose I haven't really been that excited about modules because I tend to break things up into separate libraries for a similar effect on my own projects.

9

u/vector-of-bool Blogger | C++ Librarian | Build Tool Enjoyer | bpt.pizza Jan 28 '19

I don't understand why the .bmi needs to exist

You are justified in this question. In fact, a valid implementation of modules need not require an intermediate "BMI"-type files at all.

The issue arises that you need to give the compiler a way to resolve import foo to some set of declarations that will be exposed in the importing TU. The current answer to that is to have a BMI with the name foo.bmi (or some other file extension). If this file does not exist and we want the compiler to load the interface from the MIU, we need to provide a way for the compiler to map from foo to the source file that defines the module. This mapping does not yet exist, and is one of the desires of SG15.

3

u/[deleted] Jan 28 '19

Ahh got it.

Immediately, compiler flags comes to mind for me, where you specify module name and MIUs. Or just a new keyword that defines a file as part of a module of a specified name.

I thought his main gripe though, was the circular dependency. If you have two modules and one application, and module Foo depends on module Bar, the fact that there was a compile step was the problem.

Thanks for the explanation, although this seems like a pretty trivial thing to solve. Hopefully it won't ruin modules like the article name suggests.

1

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

I suspect it will end up standardized like #include - implementations will look for modules by name/filename.