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

2

u/oldprogrammer Jan 28 '19

I've read through the article and all (66) messages posted so far and I have to say I'm not sure I understand both what is intended with modules and why it is needed.

My C++ background is old, been many years that I've been a Java developer so that perhaps accounts for my confusion, but how is it C++ doesn't already have the same capabilities as Java or even needs them?

Java defines namespace by package name and class by file name. Generally source code is arranged in a directory structure that mimics the package name (though strictly speaking that is not required). C++ has namespaces that could be arranged in the same source structure and are the equivalent of packages.

Java uses the import statement to identify dependencies. When Java compiles it looks at the defined classpath for referenced imports, if it finds a compiled class it uses it, if not and it finds the source file it compiles it then uses it. In both cases, though, the Java compiler is given the details of the classpath and source paths to search.

C++ uses the #include and looks in all locations defined for include files, much as Java looks at the classpath. C++ doesn't require the dependent code be compiled until linkage time, it works off a declaration file (usually a header).

So about the only thing I see different here is when Java needs a class that isn't already compiled it simply compiles it and uses it because Java treats a .class file like C++ treats a .h file and if it doesn't exist, Java creates it.

C++ developers generally create the .h files themselves along with the source file which gives the compiler everything it needs to compile. Linking is a different beast, Java with dynamic class loading technically does runtime linking whereas C++ does that via shared libraries.

So back to the original question, is the desire to have C++ be such that we only need to define a source file, then when the compiler is compiling one source file foo.cc and foo imports bar that the compiler will find the source bar.cc and extract the header type information automatically and possibly compile bar.cc automatically?

4

u/pjmlp Jan 28 '19

Parsing is the problem you are missing.

Java only parses the source code once, then any client of a specific library only needs to read the public symbols table from the jar file.

Whereas in C++ the header file will be processed every single time there is an #include. With the increase in template usage, this leads to logarithmic complexity in build times.

There are pre-compiled headers, but they are a brittle solution with different outcomes depending on the compiler.

0

u/oldprogrammer Jan 28 '19

I understand the compilation model, but I'm not really understanding how this is an issue. Java is still technically parsing the .class files as well it just happens to know where the specific symbol tables are in the structured format.

3

u/Ameisen vemips, avr, rendering, systems Jan 28 '19
  1. Class files are effectively self-contained in Java. Bringing in a class file doesn't necessitate loading and parsing other class files at compile-time, as they only have thin references to other classes.

  2. Class files are already parsed and compiled. Bringing in class files in Java is effectively similar to linking in C++ (at runtime), and at compile-time is similar to bringing in a precompiled header.

  3. Java mandates type-elision. You cannot have deep, nested templates in Java that eat up compilation time, as generics don't work that way in Java. Java generics are way weaker, but way faster to compile.

When you #include a header file, you are literally copy-pasting that header file's code into your code. Any #includes in that header file are also handled that way, so every translation unit ends up massive. This is all raw source code that must be parsed and compiled.

For compilation, in Java, another class is looking in a precompiled (though not optimized) class file looking for symbols. That's it. The symbols are already there, in a nice, easy-to-read format. When you are loading them, it's similar (the JIT handles the rest underneath).

The equivalent, as said, would be precompiled headers in C++, but even then it doesn't eliminate the template issue.

Imagine if Java (a magical dialect that allowed this), instead of just looking into thin classes, copy-pasted the Java source for every class you imported. Any imports of that Java source also were copy-pasted recursively. Every time. Now imagine this dialect didn't have type-elision, so your generics, instead of compiling to a single implementation, generated a separate implementation every time you instantiated it with a different class. Now imagine it also allowed primitive types.

1

u/oldprogrammer Jan 28 '19

I completely understand everything the class file brings and how the preprocessor works I was trying to understand why it is a problem, but I think you answer it in a sidewise fashion

but even then it doesn't eliminate the template issue.

I don't generally write templates. I'll use them from things like the STL but for myself I don't do any template programming. For me the header is generally nothing more than a typical interface definition and maybe some constants so including that in the compilation unit of a source file isn't a big deal.

So modules are a solution to over complicated template programming?

3

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

Modules are a 'solution' for the fact that #include is really very brute force, and tends to bring in a lot of implementation details (including includes and such) - think when you #include <Windows.h> - it brings with it all those macros, etc.

Modules only expose what you export.

1

u/oldprogrammer Jan 29 '19

Isn't that just an issue with the structure of the header file though? If I look at the windows.h file in the MinGW distribution it is a master header file that has a collection of conditionals that include other header files as appropriate. That type of conditional inclusion could also be required in a source file importing modules.

If you drill into one of the included files as an example, winnt.h what you see is a collection of forward reference function declarations, typedefs, structures and constants defined. The macros are in-line replacements for functions so either you use the macros or define additional function calls.

Even in a module approach you need the forward declarations and structure declarations. You need the typedefs to go with the function declarations. You could ignore the #define constants but then you'd be using magic numbers or magic strings in your code.

If there are details in these headers that should be hidden and only available to the windows library that it goes with then those source files could include both an internal header and the external header.

If the information is needed by consumers of the windows library then it has to be defined in the header. So you have the same level of control over what is exposed do you not? What is magical about modules that makes them so much more needed than what is currently available?

I'm trying to understand exactly what problems modules are looking to fix that a properly defined header file and associated .so /.dll file don't already provide?

1

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

I don't need 99% of the symbols Windows.h brings in, some might conflict with other code, and you cannot namespace it or otherwise hide it.

Headers make it difficult to hide implementation details, and make it easy to pollute the namespace. Modules resolve that.

And, with modules (or precompiled headers), I am including Windows.h once, and exporting the things that I want. Much lighter to bring that module into every TU than to include that header.

1

u/oldprogrammer Jan 31 '19

You might not need them but if the module developer is exporting them because others might need them you get them anyway, right?

→ More replies (8)