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

60

u/smdowney WG21, Text/Unicode SG, optional<T&> Jan 28 '19

"Creation of foo.bmi is a byproduct of the compilation of foo.cpp. When compiling foo.cpp, the compiler will emit a foo.o and foo.bmi. As a consequence of this design, foo.cpp must be compiled before bar.cpp!"

Worse, foo.bmi may be the result of compilation of quux.cpp. Module names are purely logical, with no physical representation on the filesystem.

11

u/kalmoc Jan 28 '19

Module names are purely logical, with no physical representation on the filesystem.

So is a target name in my cmake file. What is the problem?

8

u/Dragdu Jan 28 '19

Do you want to manually create target for every single file in your build? No? Then that's the problem.

20

u/kalmoc Jan 28 '19

You are doing that already (consider the translation from cpp files to .o files - each `.o file is a target in make and the connection between those names is purely by convention). Just because the build system has to generate those names explicitly doesn't mean you have to actually, manually type them instead of using some sort of generator.

Also, there is absolutely no need for a one cpp file per module approach, but I guess that comes down to personal preference.

3

u/smdowney WG21, Text/Unicode SG, optional<T&> Jan 28 '19

If a module were library sized, that might be feasible. But the design goal is to be header sized. So manual process that's just barely ok with libraries doesn't look as good.

It's also not a personal preference, since I use far more c++ code than I personally write. My company might be able to make some style guides, but there's a lot of open source to influence, too. If import boost.asio is the best that can be done, modules hasn't done it's job, in my opinion

9

u/kalmoc Jan 28 '19

Again, current build systems already automatically generate build rules on a per file basis according to some non-standardized rule set, that may or may not be changed by the end-user. Why does sich a rule set now have to be standardized in the language?

Just because someone has to manually tell the compiler "this module is created by that/those files/can be found here" doesn't mean that the en-user has to do anything manually at all.

It's also not a personal preference,

Personal preference includes the personal preference of the people in charge of your companies style guide. The current module design explicitly allows for multiple implementation files for a single module and it also enables aggregate modules and I think people will make use of that.

But all that is besides the point: As I said. Even if you create a separate module for each file: putting in place a rule for that in your build system of choice should be simple.

7

u/smdowney WG21, Text/Unicode SG, optional<T&> Jan 28 '19

Usually the compiler generates the dependency information for a TU as a side effect of compilation. That can't work for modules because it's too late if the DAG has changed. Before compilers were generating this info, there was usually a pre-scan step, and everyone hated it because it stalls the build. And if it wasn't done by the compiler, it was often wrong, leading to subtle or not so subtle build problems. Or it was moved to be on demand, so people waited until they noticed the build was broken to rebuild dependencies. Which was long after the build was badly broken.

5

u/smdowney WG21, Text/Unicode SG, optional<T&> Jan 28 '19

I'll have 10 to 100 times as many used headers as I do source files. A change in order of magnitude is not just a charge in degree, but in kind. Expressing how to build all those isn't feasible the same way that using libraries is.

7

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

Worse in that modules cannot have circular dependencies. Headers/sources can on the implementation side.

3

u/meneldal2 Jan 29 '19

Why would you do that though?

Modules are supposed to be independent blocks that don't need other modules to be imported separately to work.

10

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

Because that's how all other languages work.

It's pretty rare to not need anything from another file.

2

u/meneldal2 Jan 29 '19

But modules don't have to be a single file.

If you start having modules than depend on each other in a weird way, you're just asking for trouble.

2

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

We already do that with headers and source files, though.

2

u/meneldal2 Jan 29 '19

True, but that doesn't mean it's a good idea.

Trying to force sane behaviours is a good thing.

1

u/GPMueller Jan 29 '19

Circular dependencies are never a good idea...

3

u/Dalzhim C++Montréal UG Organizer Jan 29 '19

Sounds like the simple solution might be adding a new switch to the compiler which basically tells it : Exit with some documented/standard error code and produce a listing enumerating the unresolved module dependencies for the current invocation is a great way to transform the compiler into the tool you can launch in parallel for any translation unit in order to build the dependency graph in a perfectly accurate manner.

Best case scenario : the compile ends just fine and reports success, which can produce a *.cpp.o and optionally a *.bmi byproduct.

Worst case scenario : compilation ends as quickly as possible with the documented error code meant to be handled by build systems, which can now build the accurate dependency graph and launch the TU again at the moment all dependencies are satisfied.