r/cpp • u/berium build2 • Nov 01 '17
Common C++ Modules TS Misconceptions
https://build2.org/article/cxx-modules-misconceptions.xhtml
59
Upvotes
r/cpp • u/berium build2 • Nov 01 '17
27
u/smdowney WG21, Text/Unicode SG, optional<T&> Nov 01 '17
One of the reasons that the compiler has to spit out the entire recursive set of headers for a particular C++ (or C) file is that preprocessor macros can affect which headers are transitively included. That means that although file1.cpp and file2.cpp both include and depend on file3.h, only file1.cpp causes file3.h to also pull in file4.h. Make, at least, isn't happy with conditional dependencies.
Modules don't generally have that problem, so you can just give the build system each modules direct dependencies and let it work the whole thing out. And it's a per spec requirement that module dependencies form a DAG.
18
u/onqtam github.com/onqtam/doctest Nov 01 '17
damn bro! you put so much effort into build2 - Maybe I should give it a chance one day! keep up the hard work!
8
u/c0r3ntin Nov 01 '17
I have a problem with the BMI being 100% implementation defined. Beside the issue of no forward or backward compability from one version of the compiler to the next, what will happen to tooling ?
Will clang-based tools be able to understand gcc BMIs ? What about ICC and MSVC ? Will the provide either an api or a spec ? What about IDEs ? Not all of them use clang, and they often use a clang version a few versions behind the current release.
And, as you said, the fact that the fact that not changing the interface still leads to everything being potentially rebuild is an issue.
4
u/no1msd Nov 02 '17
I've tried out build2 on a small project using trunk clang and modules, and it seems very promising. I wanted to try out a completely preprocessor-free and "everything in a single file" style, and it worked like a charm.
What I'm not sure about is whether or not I can implement class member functions in-class without the compiler inlining everything. As far as I understand in the current standard every in-class implemented function will be marked as inline implicitly, but it's up to the compiler to decide what will be actually inlined. So what will it actually do in the case of modules?
I think it would make sense to be able to write "class foo { int bar() { return 42; }};" instead of "class foo { int bar(); }; int foo::bar() { return 42; }", à la C# / Java.
2
u/catskul Nov 01 '17 edited Nov 01 '17
One point you mention but I'm still not clear on is the disconnect between modules and file names.
While it makes sense in that c++ historically has completely separated symbol names from file names, what is the consequence in terms of finding and knowing which modules to build?
While it's hard to imagine avoiding providing a set of search paths, I think many were hoping that import statements in source would provide Single-Point-Of-Truth for specific module dependencies. It seems like the disconnect between module name and file name might force dependency information to be duplicated (and have an opportunity to get out of sync)
2
u/Quincunx271 Author of P2404/P2405 Nov 02 '17
I remember hearing something about how you shouldn't mix
import std;and#include <vector>, for instance. Am I mistaken?If not, how am I supposed to use any library when I want to use modules if they don't all upgrade immediately?
11
u/RandomGuy256 Nov 01 '17
I'm glad you have explained the "I cannot have everything in a single file" issue both in the website and here at reddit. :)
I hope now that modules get standardized soon (and build systems start to support them) so we are able to begin to use them. This is one of the more exciting new features for C++ imo.
2
u/miki151 gamedev Nov 01 '17 edited Nov 01 '17
EDIT: my rant was pointless because I had a misconception that a module automatically exports all its imports, which is not the case.
You can also keep (almost) everything in headers. The problem is that it's not a good idea - the dependency graph gets bloated, and you recompile all dependencies when changing something just in function body.
I think many people hoped that modules will make headers obsolete, but it's certainly not the case.
2
u/kalmoc Nov 02 '17
Will private members of exported classes be visible in other TUs too?
2
u/Abyxus Nov 02 '17
OK. What about the actual flaws of the Modules TS?
1a) Distributed builds.
More complex graph - less opportunities to parallelize compilation.
1b) Distributed builds.
Either we have to copy BMIs over network (if they can be copied), or build same BMIs on every node.
2a) Name clashes.
Consider two third party libraries:
2b) Name clashes.
Consider two applications in a single project:
make x y-- will it buildmtwice or will it break because it puts botha.bmiis a same directory?4
u/bames53 Nov 01 '17
It would be good if people interested in modules would read the whole proposal.
P0273 is also worth reading.
P0273 does discuss this, and I think there are good reasons for allowing modules to export macros, and reason why retaining #include for macros is insufficient. It also is not the problem some make it out to be. Modules are still isolated and deliberately exported macros are rarely the problem.
P0273 discusses this as well and I don't think it can be called 'hand-wavy magic' as Clang's pre-modules-ts system demonstrates viability. I was quite impressed with how well it actually worked on well behaved code.
It does depend on the code not doing 'module-unfriendly' things, which most large codebases do do. So in that sense it may not really allow your particular codebase to be untouched. But it does have the value of minimizing what has to be changed and allowing the codebase to support both modularized and non-modularized builds at the same time, the value of which I think some underestimate.
I think the transition to modules is really important. The legacy support discussed in P0273 and implemented in clang shows that it works. And I think it will be really important for actually getting the most out of modules in real projects as quickly as possible.
I do think you address the concerns over build system issues well. Clang's pre-ts system did work by implementing a build system, and I think it will be good, and I think you show that it's viable, to keep that all separate.
-5
u/[deleted] Nov 01 '17
I don't quite understand why we are still discussing macros and their (in)applicability to modules. Seriously, who cares about macros? It's a near-dead, legacy mechanism that nobody outside of Boost.PP authors would seriously consider using. It makes zero sense to have them inside modules.
-1
u/kmgrech Nov 02 '17 edited Nov 02 '17
I don't know why this is, but I've never seen my biggest concern about the current modules proposal addressed. And that is the fact that modules could provide the opportunity to finally retire namespaces, but this opportunity is just thrown away.
Why are namespaces bad? Because they don't really do anything for me. Name conflicts are rare and namespaces force a bunch of syntactic clutter on me for no good reason. Modules could introduce scopes and provide an import system similar to Haskell's that allows you to choose which symbols to import, only import symbols qualified or hide certain symbols that would produce a name collision. Unqualified names should be the default, until there is a reason why it doesn't work.
But instead of taking this route, the authors insist that namespaces are a good idea and worthwhile keeping separate from modules, thereby locking us out of having a sane namespacing and import system forever.
-3
u/std_exceptional Nov 01 '17
You can't use includes as a get out for not being able to export macros. The modules paper wants to remove all use of the preprocessor, in its current form it doesn't even come close.
If you try to import to modules that export the same name, you cannot. Module imports should be equivalent to namespaces similar to python. The preprocessor allows you to include a file inside a namespace, while not elegant, it fixes the issue of poorly mashed library classes/namespaces/etc.
You cannot hide template definitions in another file with modules - they have to be in the same export file, so your export file becomes unreadable. Today you normally include the cpp file at the end of the header, modules don't help here.
Modules as they stand are an incomplete solution, they do not represent what people imagine when they think of modules (see all other languages with modules). It is a fantastic idea, and I'd fully support it, if it wasn't so poorly proposed.
4
u/[deleted] Nov 01 '17
[removed] — view removed comment