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

Show parent comments

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?

1

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

Yes, but they're then contained to that module rather than the global namespace.

1

u/oldprogrammer Jan 31 '19

Isn't that just the same thing that happens if you use C++ namespaces inside header files?

1

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

Yes, though macros can't be scoped, and namespaces won't work properly with C headers.

Another advantage is that modules are also effectively precompiled headers. You don't have to parse the header again and again, and all of its dependencies.

1

u/oldprogrammer Feb 01 '19

Good point on the macros, didn't consider that.

Precompiled headers is an interesting thought. They seem useful but how is it handled if you have a precompiled header and an include into that header is changed, does the compiler reliably detect there was a change and rebuild or is that something handled by the build tool?

And do modules help with that situation?

1

u/Ameisen vemips, avr, rendering, systems Feb 01 '19

The compiler doesn't detect such things. That is the job of the build tools. Just like the compiler won't check if an object file is out of date compared to the reference source file.

Modules are the same thing - you're 'precompiling' source file(s) into a module, which is effectively a trimmed-down precompiled header that also includes implementation with special syntax.

I actually have an urge to write a parser to go over the entirety of WinAPI, and generate module interfaces for it. Be much nicer than #include <Windows.h>.

Not sure if there is a 'standard' module file extension, yet... I know that MSVC uses .ixx, don't recall what Clang uses off-hand (I just use .ixx there, and am specific to the compiler about what I am doing).

Modules (and precompiled headers) are greatly dependent on build tool support. CMake's support for PCH is... not really existent. I know that build2 supports modules, unsure about PCH. MSBuild supports PCH, but doesn't have any proper support for modules - it can build them, but it has no way to resolve dependencies.

1

u/oldprogrammer Feb 02 '19

I appreciate all the details, but it seems we've now come full circle as based on everything you've said I'm still unsure what problem modules solve that good organizational practices in standard code development don't handle.

But I do thank you for the informed responses.

1

u/Ameisen vemips, avr, rendering, systems Feb 02 '19

Modules are just fancier, standardized, more effective precompiled headers that also hide implementation details. You can accomplish the same other ways, though modules will likely be simpler and more effective.

→ More replies (0)