Any thoughts on C++ Modules

Discussion of all aspects of the game engine, including development of new and existing features.

Moderator: Forum Moderators

Post Reply
Tad_Carlucci
Developer
Posts: 503
Joined: April 24th, 2016, 4:18 pm

Any thoughts on C++ Modules

Post by Tad_Carlucci »

I read a series of blog posts (specifically, at vector-of-bool) on the ins-and-outs (and some surprising asides) of C++ Modules.

While some of the specifications are still in flux, they're setting down for c++20, and it's looking very interesting.

Of course, as reading, I was thinking of how modules might improve the quality of code for Wesnoth. Not that we could make use of them until we're ready to bump the minimum to C++20.

The idea sounds like something we should consider for not to far down the road and could start learning and planning for as compilers begin to support it.
I forked real life and now I'm getting merge conflicts.
gfgtdf
General Code Maintainer
Posts: 1344
Joined: February 10th, 2013, 2:25 pm

Re: Any thoughts on C++ Modules

Post by gfgtdf »

Well I haven't read and blogs about this but I really hope.iy will solve two of the most annoying features of c++ : the long compile times, and very annoying fact that you have to write each function interface twice: once in the header and once in the cpp file.

I'm courious whether it will imply other changes to the language I mean for example if you have two module m1, m2 ahd you have a file m2.cpp like (dont is the syntax but I suppose this is moreor less hit it will look)

Code: Select all

Import m1
Export m2

void myfunc( <complicated ye plate expression involving m> arg) {

}

will we have to write more 'hints' for the compiler to resolve the template expression without having to read m1 first.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
Tad_Carlucci
Developer
Posts: 503
Joined: April 24th, 2016, 4:18 pm

Re: Any thoughts on C++ Modules

Post by Tad_Carlucci »

As I understand it ... and remember, I've not started playing with actual code ... exporting a template is basically exporting the AST (abstract syntax tree) for use when needed. This should save compilation time by avoiding the re-parse and syntax analysis phases, at the cost of forcing every compilation unit which exports to serialize the AST (probably to a temp file) and every unit which imports it to de-serialize the AST.

In some earlier discussions people are wrapping their heads around modules by talking about it more list pre-compiled headers. But that really doesn't seem do justice to the feature.

It appears to me that modules still means seperate interface definition and declaration files, but as *.cpp, not *.hpp. (it might be wise to declare a new extension to make it clear, say *.mpp, but that would probably be a local convention.) Some examples appear to have both in the same compilation unit.

It looks like the idea of modules is to allow more fine-grained export of symbols and more general import. So `import "WML"` would bring in a number of components, possibly from a number of name spaces, from a number of compilation units, and possibly by import and re-export from other modules.

To me, this looks like an opportunity to refactor the headers more rationally. I'm pretty sure thats not really what modules are all about, but might be a beneficial side effect.

For a look, here's the vector-of-bool links:
Understanding C++ Modules: Part 1: Hello Modules, and Module Units
Understanding C++ Modules: Part 2: export, import, visible, and reachable
Understanding C++ Modules: Part 3: Linkage and Fragments

For specific questions, Stack Overflow, of course, seems to have good discussions.

For the nittgy-gritty, CPPReference.com covers C++20. It shows, at present, only CLang has support for modules (and only partial, at that).

If you're really looking to have your mind go "TILT!" there is the Technical Specification.
I forked real life and now I'm getting merge conflicts.
User avatar
loonycyborg
Windows Packager
Posts: 280
Joined: April 1st, 2008, 4:45 pm
Location: Russia/Moscow

Re: Any thoughts on C++ Modules

Post by loonycyborg »

I have high hopes for C++ module system making template metaprogramming more viable. With headers just turning a function into a template may not be viable because it would force you to move entire definition to header and thus making compile slower. If we switch to modules we can use more templates to do more useful work at compile time, thus both making resulting code faster and enabling catching more errors at compile time. Might be worth it to look into moving some wesnoth's calculations to compile time, or making more classes customizable via templates..
"meh." - zookeeper
Post Reply